3

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?

AppsDev
  • 12,319
  • 23
  • 93
  • 186

1 Answers1

0

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)

gadu
  • 1,816
  • 1
  • 17
  • 31
  • Thanks. So, does this mean I need to call `openURL:` from the caller app to "go back" to the caller app? Then it would not be like "navigating back", and it would be like opening the caller app "inside" the callee app instead? Then the complete flow would be like: caller app -> open callee app inside (so an app "inside" an app) -> callee app opens caller app inside (so an app "inside" the app that was also "inside" an app)? – AppsDev May 30 '18 at 19:58
  • right you can call `openURL:` (pretty sure), except note that its not opening it "inside" the app, it literally is just navigating to that app instead and the original app is backgrounded – gadu May 30 '18 at 20:01
  • so the caller app has no control over the code executed on the callee app unless you are able to write code for both, or, like I said, there's some sort of interface for supplying a callback URL once whatever action you're doing on the callee app is complete – gadu May 30 '18 at 20:02
  • is there a way to go back to the caller app different from calling `openURL:`? I want the same behavior as if it were the user who is tapping the "back button" with the caller app name that appears on top, and then to not have such "back button" when the caller app is shown again. But I guess that calling `openURL:` would show that "back button" on top but with the callee app name that time, right? – AppsDev May 30 '18 at 20:08
  • I don't know of any way to switch between apps that doesn't involve opening a url. You can do the app specific url (like facebook's is `fb://...`) or a universal link as long as you register it in the Associated Domains. The back button that appears at the top is like Apple level system stuff you have no control over, and being able to execute code on another app is not allowed by Apple. Once the caller app opens the callee app, it has no idea what goes on there unless the callee app decides to open the caller app. And yes the callee app will probably show up on the top once you go back. – gadu May 30 '18 at 20:34
  • The above will not work with a PWA until iOS allows it. Any suggestions for that scenario? – Matt Way Jul 09 '19 at 03:37