1
votes

I'm trying to open Amazon app from within my app using following code:

if let url = URL(string: "amzn://"),
    UIApplication.shared.canOpenURL(url) {
    UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else if let url = URL(string: "https://www.amazon.com") {
    // fallback
    UIApplication.shared.open(url, options: [:], completionHandler: nil)
}

This worked like a charm when I used it for the Youtube app. However, now with Amazon it just silently fails while it reports this error:

2018-10-11 10:38:09.794370+0200 App[9739:3023026] -canOpenURL: failed for URL: "amzn://" - error: "The operation couldn’t be completed. (OSStatus error -10814.)"

I've added url scheme to LSApplicationQueriesSchemes in Info.plist, but this changed nothing:

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>amzn</string>
</array>

What is even weirder, it does not even open the fallback URL - I would expect that if the canOpen fails, the second branch would work.

4

4 Answers

0
votes

Since iOS 9 with universal links, you should just link to the normal URL and if the app is installed on the person's device the amazon app will intercept it. See also Universal Links to Amazon.

0
votes

So after some more research, based on https://www.appsight.io it seems that the amazon app does not use "amzn://" url scheme, but "amazonToAlipay://". After changing it to this, the UIApplication.shared opens the Amazon app.

Thanks to @LinusGeffarth and his answer to another related question.

0
votes
    guard let url = URL(string: "https://www.amazon.com") else {return}

    if #available(iOS 10.0, *) {
        UIApplication.shared.open(url, options: [:], completionHandler: nil)
    } else {
        UIApplication.shared.openURL(url)
    }

If the Amazon app is installed, it should automatically open via Universal Links. https://developer.apple.com/ios/universal-links/

0
votes

The above answers did help me, however it gave me mixed results.

For some reason using the amazonToAlipay:// did not let me reach the desired product.

Using universal links, while they should work in theory, gave me mixed results. On some devices where the amazon app was installed, the amazon app opened without problems however on others, it opened in Safari even though the Amazon app was installed.

This could be because of device type, iOS versions or something else, go figure.

So as a work around, the following gave me the best results.

Using https://www.appsight.io/app/amazon as Milan suggested, I used the following scheme which was mentioned there:

com.amazon.mobile.shopping

Step 1. Whitelist this URL Scheme in your info.Plist by adding the following

<key>LSApplicationQueriesSchemes</key>
<array>
   <string>com.amazon.mobile.shopping</string>
</array>

Step 2. In my case, I needed to open a specific product on Amazon and get the product id which is present in the URL and build your string in the following format:

com.amazon.mobile.shopping://www.amazon.com/products/{your-product-id}/

Step 3. Try to open the amazon app url but also handle the case where the app might not be installed

func openAmazonProduct(withId id: String) {

   guard let amazonWebURL = URL(string: "https://amzn.to/2MQC8Bz"),
         let amazonAppURL = URL(string: "com.amazon.mobile.shopping://www.amazon.com/products/\(id)/") else {
             return
   }
        
   if UIApplication.shared.canOpenURL(amazonAppURL) {
            UIApplication.shared.open(amazonAppURL, options: [:], completionHandler: nil)
   }
   else if UIApplication.shared.canOpenURL(amazonWebURL) {
            UIApplication.shared.open(amazonWebURL, options: [:], completionHandler: nil)
   }

}