Search code examples
iosswiftdeep-linking

Unable to read incoming deep link iOS Swift


Minimum iOS version is 13, Scene delegate file is completely deleted, only using appdelegate. But open url, continue userActivity and openURLContexts methods are not calling in when app opened using deep link.

    func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
        
        print("app opened using deep link from \(sourceApplication)")
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: "DeepLink"), object: nil, userInfo: nil)
    
        return true
    }
    



    func application(_ application: UIApplication, continue userActivity:
            NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
    
        print("app opened using deep link ")
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: "DeepLink"), object: nil, userInfo: nil)
    
        return true
        }



    func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
        if let url = URLContexts.first?.url{
            print(url)
            NotificationCenter.default.post(name: NSNotification.Name(rawValue: "DeepLink"), object: nil, userInfo: nil)

        }
    }

Edit - 1

Invalid redeclaration of 'application(_:open:options:)'

enter image description here


Solution

  • Here is a Sample App I created to test deep links in minimum iOS 13 without SceneDelegate

    This is because you might be using wrong open URL method to receive deep links:
    The correct one is:

    func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
        print("Deep link received \(url)")
        return true
    }
    

    continue userActivity is generally called in the case of universal links

    func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
    }
    

    So, AppDelegate looks like:

    class AppDelegate: UIResponder, UIApplicationDelegate {
    
        var window: UIWindow?
    
        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
            return true
        }
    
        func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
            print("Deep link received \(url)")
            return true
        }
    }
    

    And in Info.plist make sure to add URL Schemes.

    <key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleURLName</key>
            <string></string>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>deepLinks</string>
            </array>
        </dict>
    </array>
    

    It looks something like this: enter image description here

    Then to test your deep links, go to your browser and write your uri scheme with ://

    Example in this case:
    deepLinks://mydeeplinkurl

    It will ask you to open your app.

    enter image description here

    And you will get a call back in open URL method

    enter image description here