Reputation: 904
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?
Upvotes: 1
Views: 861
Reputation: 318814
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.
Upvotes: 2