1

I have two apps that communicate with each other via openUrl. I want to be able to check a couple things before opening the second app.

i.e. App1 wants to launch App2. So App2 checks to see if it can open the given URL. If it can, continue with the launch. If it CAN'T, stop the launch and show an error on App1.

It seems like iOS has this functionality already, but I can't figure out how to get it to work.

This is example code from App1 that would launch App2.

let url = URL(string: "APP2://Screen1")   
if UIApplication.shared.canOpenURL(url) {
    UIApplication.shared.open(url, options: [:], completionHandler: { _ in })
} else {
    self.showError("Couldn't Open App2")
}

This is example code for App2 that would try to accept the URL.

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
    guard options[.sourceApplication] as? String == "com.test.app1" else { return false }

    if self.canLogInFrom(url) {
        return true
    else {
        return false
}

If I return false, it goes ahead with the launch anyways. It doesn't matter what I return in this function it launches App2 no matter what.

Is there a way in App2, that I can implement the AppDelegate CanOpenURL function?

SeanRobinson159
  • 894
  • 10
  • 19

1 Answers1

2

App1 can only check if App2 is installed and if it can initiate a launch and whether that initial launch succeeded.

App1 has no way to obtain the result used in the application(_:open:options:) method of App2.

Your only option would be to have App2 turn around and launch App1 with a URL that tells App1 that the launch failed for some reason.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • So what does the returned bool actually do in the `application(_:open:options:)` method then? It seems as though it is ignored. – SeanRobinson159 Apr 26 '17 at 15:38
  • It may be used by the OS in some way but the result is not passed back to App1. – rmaddy Apr 26 '17 at 15:44
  • Alright, so I technically don't know which app launched **App2** so I couldn't really jump back to the previous app if **App2** couldn't be opened. – SeanRobinson159 Apr 26 '17 at 15:46
  • The URL that App1 passes to App2 would need to include enough info such that App2 could in turn launch App1. – rmaddy Apr 26 '17 at 15:47
  • I have same issue, although I return **false** from the application(_:open:options:) method, it gets opened directly without respecting returned **false** value. I need to prevent app from opening if url is not as expected. – Dhaval H. Nena Jul 18 '18 at 05:26