My app uses in different screens:
[[UIApplication sharedApplication] openURL:url];
in order to open URLs received from my web service. Sometimes, those URLs are unrelated to my project, so opening them going to Safari is OK. Others, those URLs belong to my own, like a product detail. Those ones could be opened by my app using universal links and going to the proper screen, but that's not happening.
Reading apple docs I saw this:
It’s important to understand that if your app uses openURL: to open a universal link to your website, the link does not open in your app. In this scenario, iOS recognizes that the call originates from your app and therefore should not be handled as a universal link by your app.
https://developer.apple.com/library/archive/documentation/General/Conceptual/AppSearch/UniversalLinks.html
That behaviour confuses me. How can I use openURL to try to process first that URL inside my app instead of going directly to Safari?
I found a fast/dirty workaround. Remove openURL and use this every time my app uses openURL:
NSUserActivity* userActivity = [[NSUserActivity alloc] initWithActivityType:NSUserActivityTypeBrowsingWeb];
userActivity.webpageURL = url;
[[UIApplication sharedApplication].delegate application:[UIApplication sharedApplication] continueUserActivity:userActivity restorationHandler:nil];
Note that my appdelegate application:continueUserActivity:restorationHandler: will forward to openURL any URL that does not have a specific screen in my app, so, it is working perfectly fine, but I feel this is a terrible solution.
How do I set up Universal Links for my app? Log into your Apple developer account and go to the app's ID page. Enable the Associated Domains app service. Take note of your Prefix (bundle ID) and your ID (team ID) - you will need them later.
To support universal links in your app: Create a two-way association between your app and your website and specify the URLs that your app handles. See Supporting associated domains. Update your app delegate to respond when it receives an NSUserActivity object with the activityType set to NSUserActivityTypeBrowsingWeb .
You do this by clicking on the link that appears in the top-right corner of the screen when iOS has opened an app with a Universal Link.
Universal Links for Developers Seamlessly link to content in your app or on your website. With universal links, you can always give users the most integrated mobile experience, even when your app isn't installed on their device.
Your solution seems reasonable. Why is that terrible?
Probably you can implement more nicely looking solution, like stand-alone class LinkHandler
. The class can identify if link can be handled by your App (than it will use App's handling). Otherwise class will pass this link to iOS handling with [UIApplication openURL:]
.
A lot of Apps doing this, imagine how Google+ or Facebook iOS Apps would handle links, that posted in these social networks.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With