Search Your Question

What is Deep link in iOS?

Ans : 

Deep linking is the idea of not only having a clickable link to open up your app but a smart one that will also navigate to the desired resource.

There are 2 ways of implementing deep linking in IOS: URL scheme and Universal Links.

Comparison :


 Neither of these options will redirect you to the App Store if the app is not installed.

URL Scheme : 

Open Xcode, got to Project Settings -> Info, and add inside ‘The URL Types” section a new URL scheme. Add something of the sort of com.myApp and that’s it. Now install your app, open the app notes and type com.myApp and press enter.



Nothing will happen as IOS do not recognise this as URL. So, type com.myApp://main. Now when we press enter, we can see IOS detected the link and when we click it, a pop up will ask for permission to open MyApp from Notes.

To redirect to specific page or controller : 

Write following AppDelegate method,

  func application(_ app: UIApplication, open url: URL,
                     options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
        if let scheme = url.scheme,
            scheme.localizedCaseInsensitiveCompare("com.myApp") == .orderedSame,
            let view = url.host {
            
            var parameters: [String: String] = [:]
            URLComponents(url: url, resolvingAgainstBaseURL: false)?.queryItems?.forEach {
                parameters[$0.name] = $0.value
            }
            
            redirect(to: view, with: parameters)
        }
        return true
    }


and open com.myApp://profile?user=”mananshah”  from notes app.
Here,
     url.scheme = “com.myApp”
     url.host = “profile”
     parameters = [ “user” : “mananshah” ]

This URL will not work if app is not installed. If app is not installed, this will open safari and show blank page. For solving, we should implement Universal link.

Universal Link : 

Universal links are a bit more complex. Basically, we want IOS to relate a webpage URL to our app. Hitting URL expect a json in following format to ensure valid apps :

{
        “applinks”: {
            “apps”: [],
            “details”: [
                {
                    “appID”: “T5TQ36Q2SQ.com.myapp.production”,
                    “paths”: [“*”],
                }
            ]
        }

    }

Applinks indicate this is indeed for the Universal Link declaration.
Apps should be left as an empty array. This is most likely because these types of JSON are used for other purposes other than universal links as well.
Details will then contain an array of your apps and the mapping of each subpath to the respective app.
For each app, you should add a field called appID which is obtained concatenating your teamID and your app’s bundleID. For example, if your TeamID is 123456 and your AppID is com.myApp, then the result is 123456.com.myApp.
In the paths field, an array of strings representing with expressions of the paths which correspond to this app. For example, if you want myApp.com/store to open up a different app than myApp.com/maps, here you can declare that is a wildcard for any string, while ? is a wildcard for any character. As we only have one app, any subpath will lead here, hence the *. If you want to exclude a subpath, just add NOT at the beginning.
-----
Once that is done, we now must add the correct metadata to the app. 
First, open Xcode, go to Project settings -> capabilities. Scroll down to Associated Domains and turn it on. Once it is enabled, we shall add any URL that implements our apple-app-site-association resource, preceded by app links. Inside the Domains section, add a applinks:myApp.com. Once this is done, go ahead and try out your app.
To redirect to specific page or controller : 


public func application(_ application: UIApplication,
                               continue userActivity: NSUserActivity,
                               restorationHandler: @escaping ([Any]?) -> Void) -> Bool {
           if let url = userActivity.webpageURL {
               var view = url.lastPathComponent
               var parameters: [String: String] = [:]
               URLComponents(url: url, resolvingAgainstBaseURL: false)?.queryItems?.forEach {
                   parameters[$0.name] = $0.value
               }
               
               redirect(to: view, with: parameters)
           }
           return true
      }



Universal links allow us to have a fallback webpage if a user does not have the app installed
if the user has the app, the link will open it, and if not, it will fall under the webpage.

Credit : WOLOX

No comments:

Post a Comment

Thanks