2

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.

Milan Nosáľ
  • 19,169
  • 4
  • 55
  • 90
  • please check this answer:-https://stackoverflow.com/questions/43000359/open-amazon-link-with-amazon-app-not-safari-ios-swift-3. – V D Purohit Oct 11 '18 at 10:04

5 Answers5

2

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)
   }

}
Shawn Frank
  • 4,381
  • 2
  • 19
  • 29
1

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.

Milan Nosáľ
  • 19,169
  • 4
  • 55
  • 90
0

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.

Steven Gurr
  • 101
  • 3
  • When I try to open link `"https://www.amazon.com"` directly, it just opens safari with amazon website - and I have amazon app installed on the device. – Milan Nosáľ Oct 11 '18 at 09:21
  • Playing around with some other apps, it seems that for some it works (e.g., twitter or facebook) and for some not, and I have to use url scheme (e.g., snapchat, and amazon - which I cannot get working using the scheme, as question states) – Milan Nosáľ Oct 11 '18 at 09:33
  • Have you checked for the banner at the top of the page as mentioned here: https://stackoverflow.com/a/35614651/10485169? – Steven Gurr Oct 11 '18 at 09:36
  • yep, there is no banner.. the banner appeared for snapchat, but this does not work as I want it to work - I don't want the user to tap twice, when there should be just "one-tap" way.. – Milan Nosáľ Oct 11 '18 at 09:38
  • Yeah it would be one click with universal links, as you've seen with Twitter. The mechanism is the same. Could you try on another device, or delete the amazon app and re-install it? – Steven Gurr Oct 11 '18 at 09:42
0
    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/

Gibraltar
  • 1,106
  • 9
  • 21
0

I know this is old, but all of this information is wrong. There’s no sure fire way to open an app with a universal link on user’s device. Universal links are a per-app option set by the user on a given device. They’ve been that way since the beginning. You can test this out by long-pressing on a universal link and seeing the options open on where to handle that URL. Once a user sets that, all universal links for that app will follow the user’s decision from that point on. The only way to definitely open another installed app is with a deep link, however, that app has to be installed for it to do anything. Since there are some innate insecurities with deep links, companies won’t likely pass around their URL schemes or deep link formatting. A few large companies have information on their websites on how to link to their apps, but outside of that, you’re just hacking at solutions that will sometimes work for some users on some devices. Even if your just trying to link to your own app from somewhere like Twitter or Facebook, most apps like that open URLs in webviews within their app, and not directly with safari, so universal links and deep links don’t work at all there.

Nick Sinklier
  • 221
  • 1
  • 3
  • 9