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

前端 未结 2 1089
梦毁少年i
梦毁少年i 2021-01-17 07:39

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 w

相关标签:
2条回答
  • 2021-01-17 08:25

    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
    }
    
    0 讨论(0)
  • 2021-01-17 08:27

    @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 
    
    }
    
    0 讨论(0)
提交回复
热议问题