6

I working on Universal links implementation for our iOS app.

Here is my small piece of AppDelegate:

private func application(_ application: UIApplication, openURL url: URL, sourceApplication: String?, annotation: AnyObject) -> Bool {
    DeepLinkHelpers.handleUniversalLink(url.absoluteString)
    return true
}

private func application(application: UIApplication, continueUserActivity userActivity: NSUserActivity, restorationHandler: ([AnyObject]?) -> Void) -> Bool {
    DeepLinkHelpers.handleUniversalLink(userActivity.webpageURL?.absoluteString)
    return true
}

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

    signalRConnector.launch()

    NotificationCenter.default.addObserver(self, selector: #selector(AppDelegate.processRestartSignalRNotification(_:)), name: NSNotification.Name(rawValue: "restartSignalR"), object: nil)

    NotificationCenter.default.addObserver(self,                            selector: #selector(AppDelegate.reachabilityChanged(_:)),
                                           name: ReachabilityChangedNotification,
                                           object: reachability)

    do {
        try reachability.startNotifier()
    } catch {
        Logger.save(text: "Unable to start notifier")
    }
    return true
}

I have processed all other steps for universal links integration:

  1. Published apple-app-site-association file in our Web application
  2. Switched on Associated domains feature on developers.apple.com
  3. Specified associated domains in xcode
  4. Checked Target Membership for entitlements file
  5. Said xcode to wait until app will be started manually (instead of autostart)

I doing following to debug:

  1. Connecting ipad
  2. Starting project in xcode
  3. In ipad opening Calendar and click on link contained in some event. Link has following format: app.domain.com/#/123456789
  4. Ipad opens app but continueUserActivity not calling and i can't handle code from the url for navigate to exact state within an app.

According documentation continueUserActivity should be executed. It is not executing both when app is running in background and when app not running.

Thank you in advance! Any help appreciated.

dskibin
  • 165
  • 1
  • 2
  • 11
  • https://search.developer.apple.com/appsearch-validation-tool gives me error: Unable to parse that webpage URL. Try a different URL. – dskibin Dec 16 '16 at 20:07
  • but this one says the all correct and all tests are green: https://branch.io/resources/aasa-validator – dskibin Dec 16 '16 at 20:08
  • The Apple validator is _not_ a Universal Links validator and its results have no connection to whether Universal Links function or not. See [here](http://stackoverflow.com/questions/41169954/applinks-validator-domain-missing-from-entitlement/41173007#41173007) for more details on that. – Alex Bauer Dec 17 '16 at 17:13

4 Answers4

23

As of iOS 12, Swift 4.2, and Xcode 10 Apple has again changed method signature (type of restorationHandler) to

 func application(_ application: UIApplication, continue userActivity: 
NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
Michał Ziobro
  • 10,759
  • 11
  • 88
  • 143
4

after ios 13 you should use ScenDelegate:

//when app is running

func scene(_ scene: UIScene, continue userActivity: NSUserActivity)

//when app is not running 
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions)
Azade Rahmati
  • 135
  • 1
  • 5
3

I suspect this is because you've marked the continueUserActivity function as private. I've never seen that, and in fact Swift 3 actually gives an error when I try. There don't appear to be any examples of code structured that way on all of GitHub.

I'd suggest removing the private scope.

Alex Bauer
  • 13,147
  • 1
  • 27
  • 44
  • 1
    Thanks Alex! I have changed my code to be: func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool { ... return true } And it works now. – dskibin Dec 18 '16 at 18:10
  • Thanks Alex, must have copied a bogus tutorial. – William T. Mallard Dec 14 '18 at 06:57
1

In 2019, this is what helped me (from this guide: https://medium.com/@abhimuralidharan/universal-links-in-ios-79c4ee038272 ):

func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {
    print("Continue User Activity called: ")
    if userActivity.activityType == NSUserActivityTypeBrowsingWeb {
        let url = userActivity.webpageURL!
        print(url.absoluteString)
        //handle url and open whatever page you want to open.
    }
    return true
}
Starwave
  • 2,352
  • 2
  • 23
  • 30