Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Custom Url Scheme XCode

Tags:

xcode

ios

First of all, I know how to make custom schemes in iOS and I know how to open my app from a website using a javascript setTimeout method.

I have an app that uses custom URL scheme and it is working great. What it does is, it sends a http://testsite.com/QueryStrings message to other users in the contact list (predefined) and on clicking those web links in the sms, these things happen:

  1. Open the link in Safari
  2. Open the app if installed with custom url using setTimeout
  3. If not installed, move to the normal website page

What I wanted actually is to open my app directly from SMS if installed but for that I have to send my custom url scheme in the SMS, that is not an option because if app is not installed then this SMS wont work so a weblink is the only option for now.

Today, I installed SoundCloud and accidentally noticed this thing is that when http:// m. soundcloud .com /... url is sent in an SMS and on clicking the link it opens the app (if installed) directly not the Safari (Strange for me).

So I was wondering how come their app open from a web link without opening the Safari. I googled it around but I couldn't find a solution to my problem. I am attaching a screenshot too from my mobile where press and hold on the link in the messages app give Open in "SoundCloud" option as well. So how SoundCloud registered a http link to be handled automatically in the app. Please help guys

Screenshot of SoundCloud Open

like image 611
Sarmad Makhdoom Avatar asked Oct 30 '22 10:10

Sarmad Makhdoom


1 Answers

The answer to this problem is using Associated Domains (But after 9.2 we have to use Universal Links to achieve this).

Before Universal Links, the primary mechanism to open up an app when it was installed was by trying to redirect to an app’s URI scheme (registered in the app’s PLIST like so) in Safari. This put the routing logic in Safari, but there was no way to check if the app was installed or not.

iOS 9 Universal Links were intended to fix this. Instead of opening up Safari first when a link is clicked, iOS will check if a Universal Link has been registered for the domain associated with the link, then check if the corresponding app is installed. If the app is currently installed, it will be opened. If it’s not, Safari will open and the http(s) link will load.

Functionally, it allows you have a single link that will either open your app or open your mobile site.

Configure your app to register approved domains

  1. Registered your app at developers.apple.com
  2. Enable ‘Associated Domains’ on your app identifier
  3. Enable ‘Associated Domain’ on in your Xcode project
  4. Add the proper domain entitlement
  5. Make sure the entitlements file is included at build

Configure your website to host the ‘apple-app-site-association’ file

  1. Buy a domain name or pick from your existing
  2. Acquire SSL certification for the domain name
  3. Create structured ‘apple-app-site-association’ JSON file
  4. Sign the JSON file with the SSL certification
  5. Configure the file server

Apple launched Universal Links in iOS 9.0, which moves the app routing into the OS so that developers don’t need to worry about doing the routing in Javascript.

Receiving Universal Link URL in the App

URI schemes received the deep link URL through openUrl in the App Delegate. Universal Links receive their data via a different code path: continueUserActivity. This new delegate method is used for a number of app transitions, ranging from Spotlight to Universal Links, and will likely see a couple more use cases introduced in future OS versions.

Below is a snippet of code that you can use to retrieve the full Universal Link URL that opened the app.

- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray *))restorationHandler {
    if ([userActivity.activityType isEqualToString:NSUserActivityTypeBrowsingWeb]) {
        NSString *myUrl = [userActivity.webpageURL absoluteString];
        // parse URL string or access query params
    }
    return YES;
}

Source: https://blog.branch.io/how-to-setup-universal-links-to-deep-link-on-apple-ios-9

like image 113
Sarmad Makhdoom Avatar answered Nov 11 '22 09:11

Sarmad Makhdoom