iOS Universal Link opens app, does not trigger app delegate methods
Asked Answered
F

4

10

I am trying to enable universal links on iOS, (as part of Firebase's password-less sign-up). Testing locally on iOS 13.2.

The apple-app-site-associated (AASA) JSON looks as such (https://lokitools.page.link/apple-app-site-association):

{"applinks":{"apps":[],"details":[{"appID":"43S54AHEMG.com.jkalash.Loki","paths":["NOT /_/*","/*"]}]}}

Universal links do open the app, however I am unable to handle the app opening from them. Delegate methods:

  1. application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool
  2. application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool

do not get called, when opening from universal links. Tried both apps running in the background and force closed. AASA validator (https://branch.io/resources/aasa-validator/) says file looks good, and I have tried troubleshooting by re-installing app and observing console logs for swcd (https://ios13.dev/universal-links-debugging-on-ios-13-cjwsux93w001p6ws1swtstmzc) but nothing out of the ordinary shows up and it does look like the AASA file was downloaded.

I have also tried following Apple's troubleshooting guide (https://developer.apple.com/library/archive/qa/qa1916/_index.html) but the final step which fails (step 8) does not cover my case which is the app does open (iOS detects universal links), but the delegate methods just don't get called.

Fucoid answered 1/1, 2020 at 12:44 Comment(4)
What does your didFinishLaunchingWithOptions method in AppDelegate return?Baillieu
It always returns trueFucoid
In the case of Facebook integration, there is an issue. If you are returning true, then it might be a different issue. Let me share my working delegate method.Baillieu
I have exactly the same problem except in iOS 12...Wayland
F
17

Turns out this is not a universal links specific problem, but a change in iOS 13's way of triggering app lifecycle events. Instead of coming through UIApplicationDelegate, they come through UISceneDelegate.

One confusing thing is that the app delegate methods aren't deprecated so you won't get a warning if you have both app delegate and scene delegate methods in place but only one will be called.

Refer to App delegate methods aren't being called in iOS 13 for a comprehensive answer

Fucoid answered 1/1, 2020 at 13:23 Comment(0)
S
6

I am using iOS 13 with Swift 5, replace the application (: continue: restorationHandler :) method of the AppDelegate.swift file and add the scene (: continue :) method to the SceneDelgate.swift file

In my case in the SceneDelegate.swift file add the following:

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

    guard userActivity.activityType == NSUserActivityTypeBrowsingWeb,
        let urlToOpen = userActivity.webpageURL else {
            return
    }

    handleURL(urlToOpen)
}
Shirtwaist answered 6/5, 2020 at 21:34 Comment(0)
B
2

Since you are able to open the app, I think all is good with your AASA file. The following delegate method gets called fine in my case:

func application(_: UIApplication, continue userActivity: NSUserActivity, restorationHandler _: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
    guard userActivity.activityType == NSUserActivityTypeBrowsingWeb,
        let url = userActivity.webpageURL else {
        return false
    }
    let urlString = url.absoluteString

    var queryParams: [String: String?] = [:]
    if let components = NSURLComponents(url: url, resolvingAgainstBaseURL: true),
        let params = components.queryItems {
        for param in params {
            queryParams[param.name] = param.value
        }
    }
    return true
}

Hope it helps!

Baillieu answered 1/1, 2020 at 13:2 Comment(7)
Mentioned it in the question already, the following delegate method does not get called when app opened through universal link.Fucoid
I wish this doesn't become a long debugging new year day my friend! I hope you resolve it fast. I would just request you to replace your implementation with this one and check one more time, please.Baillieu
A minimal version of your AppDelegate may be worth looking at.Baillieu
figured it out and posted an answer, thanks for the help though!Fucoid
Awesome! Glad to be of some help :)Baillieu
Hi Josh! Did you have any Application Scene Manifest entry on your .plist or you implemented the new UISceneDelegate ?Iseabal
@joe this method is called only when the app is available in the device, what if the app is not in the phone and you installing the app in by clicking the universal link. will this method redirection function is works for you?Leven
H
0

If you use Google Analytics, please refer to my here. The issue may be caused by method swizzling.

Heilman answered 15/9, 2020 at 4:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.