loading url from scheme not processing first time - appdelegate vs viewcontroller

旧街凉风 提交于 2019-12-01 13:30:46

问题


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.

My code looks as follow:

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

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
    }
}

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. 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.


回答1:


Copy your code from

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

into

func application(_ application: UIApplication, 
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) 
    -> Bool {

In that method, check the launchOptions. If it contains the url key, grab that value — and return false.

if let url = launchOptions[.url] as? URL {
    let geturl = url.host?.removingPercentEncoding
    UserDefaults.standard.set(geturl, forKey: "DeepLinkUrl")
    return false
}



回答2:


@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.

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.

Try implementing the appDelegate's

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

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 

}


来源:https://stackoverflow.com/questions/55880945/loading-url-from-scheme-not-processing-first-time-appdelegate-vs-viewcontrolle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!