Universal Link opens my app but doesn't call application:continueUserActivity:restorationHandler
Asked Answered
P

3

10

I'm trying to implement Universal Links in my app. I read a lot of tutorials and followed this one to the letter: How to support Universal Links in iOS App and setup server for it?

When I click a universal link my app successfully opens but application:continueUserActivity:restorationHandler in my AppDelegate.m file isn't called so I can't direct to a specific page in the app.

My apple-app-site-association file is on https and for paths I put [ "*", "/" ]. I triple-checked app prefix and ID, confirmed AssociatedDomains is enabled both on the developer website and in my target. In the Associated Domains section I put my root domain as well as a second entry prepended with *. to handle all possible subdomains. I am testing on a real device, not on the simulator.

-(BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler {
    ALog(@"Did I get here?");
}

Can anyone provide advice as to what I should be looking at to figure out why application:continueUserActivity:restorationHandler isn't being called?

Update:

I used Apple's validation tool https://search.developer.apple.com/appsearch-validation-tool and it gave this result:

Link to Application

Action required

Could not extract required information for Universal Links. Learn how to implement the recommended Universal Links.

Error no apps with domain entitlements The entitlement data used to verify deep link dual authentication is from the current released version of your app. This data may take 48 hours to update.

I don't know if that has anything to do with my problem. I wouldn't think so, because I need to test my app to make sure I have Universal Links implemented properly before actually releasing the update.

Preussen answered 8/8, 2016 at 21:56 Comment(10)
Since your app is opening, you've already passed 99% of the usual problems with Universal Links. You don't need to worry about the message from Apple's validation tool — I've almost never seen that not present, and it is there on many implementations that work perfectly. What method are you using to be certain that the continueUserActivity handler isn't called?Somersomers
I put an ALog statement on the first line which never gets printed. I did find that application:openURL:sourceApplication:annotation gets called, but again have no idea why. According to the documentation application:continueUserActivity:restorationHandler should be called.Preussen
Hmm. You are testing this on a physical device and not the simulator, correct? We might need to see an actual link exampleSomersomers
The links are in the form of mydomain://phase1/phase2?key1=var1&key2=var2#hash. My current workaround is to just use application:openURL:sourceApplication:annotation but I'd still like to know why that is triggering and application:continueUserActivity:restorationHandler is not.Preussen
Hard to tell from that example — looks almost like a uri scheme, in which case openURL would be correct. Could we get a full example of an actual link?Somersomers
I think I may be misunderstanding universal links. I was using MyCustomSceme://phase1/ which I guess is only for deep linking... so universal links are supposed to look like web links, e.g. http://example.com/phase1/? If that's the case then I'm not as close to figuring it out as I thought, because those links do not open my app.Preussen
Ahh, yes. Those are URI scheme links. Universal Links are normal http links. Here is a good overview: blog.branch.io/…Somersomers
@Preussen if you have Google Analytics and Localytics implemented in your app, you might want to take a look this git issue: github.com/BranchMetrics/ios-branch-deep-linking/issues/485Garrott
I know it's an old question but did you fix the issue at the end? I have exactly the same problem :(Lands
Unfortunately not.Preussen
S
1

If Google Analytics or some Firebase features is used in the app, it may be caused by method swizzling. Adding the following method in application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) solves the issue for me. I think disabling method swizzling will work too.

private func swizzleSelectors() {
    
    let aClass: AnyClass! = object_getClass(UIApplication.shared.delegate)
    let originalMethod = class_getInstanceMethod(aClass, #selector(self.application(_:continue:restorationHandler:)))
    let swizzledMethod = class_getInstanceMethod(aClass, #selector(UIApplication.shared.delegate!.application(_:continue:restorationHandler:)))
    if let originalMethod = originalMethod, let swizzledMethod = swizzledMethod {
        method_exchangeImplementations(originalMethod, swizzledMethod)
    }
}
Sulfapyridine answered 15/9, 2020 at 4:17 Comment(0)
A
1

Nowadays the problem could be that the message is not sent to AppDelegate, but to its successor: scene delegate. So use then this method instead:

UIWindowSceneDelegate::scene(_ scene: UIScene, continue userActivity: NSUserActivity)
Allowedly answered 29/11, 2021 at 7:54 Comment(0)
A
0

I was struggling with this issue in SwiftUI in iOS 17 - none of the AppDelegate/SceneDelegate "url handling" callbacks worked. Solution that worked:

struct MyApp: App {
var body: some Scene {
    WindowGroup {
        RootView()
            .onOpenURL { incomingURL in
                print("URL: \(incomingURL.absoluteString)")
            }
        }
    }
}

But I believe such a handler can be attached to any view, while that view is present in the hierarchy.

Aegisthus answered 9/7 at 12:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.