从第一次未处理的方案中加载 url - appdelegate vs viewcontroller [英] loading url from scheme not processing first time - appdelegate vs viewcontroller

查看:17
本文介绍了从第一次未处理的方案中加载 url - appdelegate vs viewcontroller的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序已成功打开并将参数(来自 URL 方案,即 myApp://sometextToPrint)设置为 AppDelegate 类中的变量,但每当我想处理它们时,它首先失败从该 URL 打开应用程序的时间.我在前台检查器中有一个应用程序,它调用函数来打印给定的参数,但不知何故,AppDelegate 的加载时间比视图晚,为什么视图在第一次加载时无法打印参数.

My app is successfully opening and setting the parameters (from URL-scheme i.e. myApp://sometextToPrint) to a variable in the AppDelegate class but whenever I want to process them it fails at the first time when the app is opened from that URL. I have the app in foreground checker that calls the function for printing the parameters given, but somehow it looks like the AppDelegate is loaded later than the view why the view fails to print the parameters at first load.

我的代码如下:

AppDelegate.m

AppDelegate.m

func application(_ app: UIApplication, open url: URL, options: 
[UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {

let geturl = url.host?.removingPercentEncoding;
UserDefaults.standard.set(geturl, forKey: "DeepLinkUrl")
return true
}

ViewController.m

ViewController.m

override func viewDidLoad() {
    super.viewDidLoad()
    NotificationCenter.default.addObserver(self, selector: #selector(appWillEnterForeground),name: UIApplication.willEnterForegroundNotification, object: nil)

}

@objc func appWillEnterForeground() {
    print("app on foreground")
    let user = UserDefaults.standard
    if user.url(forKey: "DeepLinkUrl") != nil {

    let str = user.value(forKey: "DeepLinkUrl") as! String
    print(str) //this is printed on the second time when I click home button and open app again
    }
}

例如,如果我转到 Safari 并点击以下 URL:myApp://printthistext,则不会打印文本.当我单击主页按钮并再次单击应用程序时,它会打印出来.

For e.g., if I go to Safari and hit the following URL: myApp://printthistext, the text is not being printed. It prints when I click the home button and click the app again.

附言我是 swift 的新手,还不知道首先加载/处理哪个类(AppDelegate 或 ViewContoller).我已经尝试了将近 5 个小时来解决这个问题,第二次仍然保持打印.

P.s. I am new at swift and don't know exactly yet which class (AppDelegate or ViewContoller) is loaded/processed first. I am already trying almost 5 hours to fix this, still keeps printing for the second time.

推荐答案

@SalihanPamuk 我认为这是由于 appWillEnterForeground() 方法造成的.UIApplication.willEnterForegroundNotification - 这是发布应用程序进入前台的第一个通知.

@SalihanPamuk I think this is due to the appWillEnterForeground() method. UIApplication.willEnterForegroundNotification - This is the first notification posting an app is coming to the foreground.

因为您已经注册了一个监听器来监听这个通知,appWillEnterForeground() 方法将在 appDelegate 的

As you have already registered an observer for listening to this notification the appWillEnterForeground() method will invoke before the appDelegate's

application(_ app: UIApplication, open url: URL, options: 
[UIApplication.OpenURLOptionsKey : Any] = [:]) - method.

尝试实现 appDelegate 的

Try implementing the appDelegate's

func applicationWillEnterForeground(_ application: UIApplication) {
    // Do your stuffs here 
}

或者,如果您想在使用任何 URL 设置打开应用程序后显示任何特定的 ViewController,请使用用于在其中显示该 ViewController 的代码

Or if you want to show any particular ViewController after opening the app using any URL setup the code for showing that ViewController inside the

func application(_ app: UIApplication, open url: URL, options: 
[UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {

// implemet your code here 

}

这篇关于从第一次未处理的方案中加载 url - appdelegate vs viewcontroller的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆