Colin Colin - 1 year ago 331
iOS Question

iOS: apple universal link if app is not open?

My app can successfully handle apple universal links, if the app is already open (backgrounded). But if the app is not open already, then when I tap such a link in, say, mail, the app opens, but I never get the callback for application:continueUserActivity... (which I do if the app was already open/backgrounded)...

To wit:

If the app is backgrounded, and I click on an apple universal link in, say, the mail app, then this method (which is what apple's documentation says to implement to handle universal links):

optional func application(_ application: UIApplication,
continueUserActivity userActivity: NSUserActivity,
restorationHandler restorationHandler: ([AnyObject]?) -> Void) -> Bool

Gets called. If the app is not running (I force close it), then when I click on the link, that method does NOT get called, but the app DOES open.

Is this supposed to work this way?

Based MCMatan's clue, you have to do something like this in didFinishLaunchingWithOptions, and then continueUserActivity will get called:

if let userActivityDict = launchOptions?[UIApplicationLaunchOptionsUserActivityDictionaryKey] as? NSDictionary,
activityType = userActivityDict[UIApplicationLaunchOptionsUserActivityTypeKey] as? String where activityType == NSUserActivityTypeBrowsingWeb {

return true

Answer Source

You are correct. If the app is not in background "continueUserActivity" will not be called.

Instead, application:didFinishLaunchingWithOptions will call with the information:

let activityDic = launchOptions?[UIApplicationLaunchOptionsUserActivityDictionaryKey]
if let isActivityDic = activityDic {
    // Continue activity here