2

I have been following this link to implement interapp two way communication using x-callback-url. So I made two different app - SourceApp & TargetApp.

SourceApp

The URL Scheme: enter image description here

And implementation to open TargetApp as follows:

@IBAction func btnOpenAppPressed(_ sender:UIButton){

        let url = URL.init(string: "targetapp://x-callback-url/translate?x-success=sourceapp://x-callback-url/acceptTranslation&x-source=SourceApp&x-error=sourceapp://x-callback-url/translationError&word=Hello&language=Spanish")

        if (UIApplication.shared.canOpenURL(url!)){

            UIApplication.shared.open(url!, options: [:], completionHandler: nil)
        }
}

AppDelegate method to receive the response back from TargetApp:

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {

        print("Response From TargetApp==>\(url.absoluteString)")
        return true
}

TargetApp

The URL Scheme: enter image description here

AppDelegate method to receive the request from SourceApp:

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {

        print("Response From SourceApp==>\(url.absoluteString)")

        return true
} 

IBAction of TargetApp to send the response back to SourceApp:

@IBAction func btnBackToSourceAppPressed(_ sender:UIButton){

        let url = URL.init(string: "sourceapp://x-callback-url/acceptTranslation?x-source=TargetApp&word=Hola")

        if (UIApplication.shared.canOpenURL(url!)){

            UIApplication.shared.open(url!, options: [:], completionHandler: nil)
        }
}

Now the problem is, I can open the TargetApp from SourceApp but can't return from TargetApp to SourceApp. I even looked into this approach but I found it same as mine.

Any help will be appreciated.

Poles
  • 3,585
  • 9
  • 43
  • 91

1 Answers1

1

After 2 days of struggle I found that I was not using LSApplicationQueriesSchemes in plist. I also found that in Objective-C if I skip LSApplicationQueriesSchemes I can easily communicate between those two apps. But if you are using swift you must you LSApplicationQueriesSchemes otherwise, you will get

-canOpenURL: failed for URL: "targetapp://" - error: "This app is not allowed to query for scheme targetapp"

So, I had to use

<key>LSApplicationQueriesSchemes</key>
    <array>
        <string>targetapp</string>
    </array>

in SourceApp's plist and

<key>LSApplicationQueriesSchemes</key>
    <array>
        <string>sourceapp</string>
    </array>

in TargetApp's plist.

I made two demo apps that easlily demonstrates inter app two-way communication using x-callback-url.

Poles
  • 3,585
  • 9
  • 43
  • 91