Universal links: how to go back programmatically?

1.7k views Asked by At

I have a first iOS native app (let's say the "caller app") that opens a second one (let's say "callee app") with an URL of the form https://calleeapp.com (Universal Link). I want the user to perform some task in the "callee app", and when finished then I want to go back automatically to the "caller app" providing the result of such task (URL would be then something similar to https://callerapp.com/check?result=xxxx).

What should be the best/appropriate way to handle this?

1

There are 1 answers

6
gadu On

We'll, you'd need to be able to have code on both the callee and caller app then to do this. (Unless a third party app implements some sort of API for callback URLS).

On the caller app though you'd want to look at the following AppDelegate method:

func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {
    // if it opened the app from some link
    if userActivity.activityType == NSUserActivityTypeBrowsingWeb {
        // the query will give you "result=xxxx"
        if let query = userActivity.webpageURL?.query {
             // code that pulls out the result from the string you get back
        }
     ...

You also have to actually support the universal link by making sure that your AppName.entitlements has the an Associated Domains key with one of the values being applinks:callerapp.com so that the caller app knows it can open urls in that scheme. (And vice versa on the callee app if you're also implementing that)