24
votes

This is the code I use:

let instagramURL = NSURL(string: "instagram://app")
if UIApplication.shared.canOpenURL(instagramURL! as URL) {
  //Code
} else {
  //Showing message "Please install the Instagram application"
}

I am getting unsuccessful to enter in if loop.

I get this error:

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

I have also Login with Instagram in my device.

4
UIApplication.shared.canOpenURL checks to see if your app can open the URL. The error indicates that you're not allow to check if your app can open an Instragram URL (because you could write malicious code to intercept URLs intended for other apps). What are you trying to accomplish? - Anna Dickinson

4 Answers

53
votes

Right click on your plist file and open it as source code. Then copy and paste below code:

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>instagram</string>
</array>

Note: One thing you have to keep in mind that it will not work on simulator. You need a real device for this.

10
votes

Open your plist as source code and paste following code:

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>instagram</string>
</array>
9
votes

The issue is that you are not registering the URL scheme in the info.plist file.

Please add this LSApplicationQueriesSchemes and add instagram in your info.plist and it will work. enter image description here

4
votes

For those who try to open the app using custom URL Scheme (assume that app FirstApp opens SecondApp):

  • In FirstApp add LSApplicationQueriesSchemes with URL Scheme to Info.plist like this:

enter image description here

  • In SecondApp register new URL Scheme in URL Types like this:

enter image description here