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

查看:90
本文介绍了从方案加载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.

P.s.我是新手,还不完全知道哪个类(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天全站免登陆