Search code examples
iosswifturl-schemeopenurl

Can't use canOpenUrl for InterApp communication


I would like to communicate between two Apps installed on the same phone. In order to do this, I read many official documents to understand that I have to implement a Custom URL Scheme.

Before iOS 9, it seems that we have to add a URLType in Info and define the URL Scheme : " ".

But after the iOS 9, it change the way to communicate between Apps.

The url scheme example is discussed in: Querying URL Schemes with canOpenURL.

My App A code below:

@IBAction func sender(sender: AnyObject) {

    let ourapplication : UIApplication = UIApplication.sharedApplication()
    let ourpath : String = "iOSTest://"
        //.stringByAppendingString(urlEncodedText)
    let oururl : NSURL = NSURL(string: ourpath)!

    ourapplication.canOpenURL(oururl)
} 

At My App B, I add a url name iOSTest in Info.plist

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>iOSTest</string>
</array>

When I install the two App on my iPhone to test, it doesn't work at all.

Here is my error!

Error photo

What's wrong with my App?


Solution

  • You have it setup incorrectly. The app calling canOpenURL is the app that needs to add the custom scheme to the LSApplicationQueriesSchemes list.

    Since App A is calling canOpenURL for iIOSTest, it is App A that needs to add iOSTest to the LSApplicationQueriesSchemes list, not App B.

    App B would be the app that needs to register that it responds to iOSTest so that it will be opened when some other app calls openURL with a scheme of iOSTest.