3

I am trying to share Images on Instagram, it's working fine if Instagram is installed on my device, otherwise it will open another app which has the same functionality as Instagram sharing (instead of error throwing).

My Instagram sharing Code is:

let instaFilePath = "instagram://library?AssetPath=\(url!.absoluteString)&InstagramCaption=SocialCommerce"
let instaFilePathURL = URL(string: instaFilePath)
let instagramUrl = URL(string: "instagram://app")

if UIApplication.shared.canOpenURL(instagramUrl!) {
    if #available(iOS 10.0, *) {
        UIApplication.shared.open(instaFilePathURL!, options: [:], completionHandler: { (completed) in
            print("-----------------------(-)--------------------------\(completed)")
        })
    } else {
        self.showMessage("This feature is not available earlier version then iOS 10.0")
    }
} else {
    self.showMessage("Instagram is not present in your device")
}

I have used CFBundleURLSchemes and LSApplicationQueriesSchemes scheme in Info.plist

 <dict>
    <key>CFBundleURLSchemes</key>
    <array>
        <string>instagram</string>
    </array>
</dict>

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

Does anyone have a solution for this? I can't post images to Instagram when Instagram is not installed, I can't even detect if Instagram is installed or not.

Following custom URL Scheme from: https://www.instagram.com/developer/mobile-sharing/iphone-hooks/

Hmax
  • 314
  • 6
  • 16
Raj Joshi
  • 2,669
  • 2
  • 30
  • 37

1 Answers1

1

First of all, you need to understand Info.plist! You are telling to the compiled your app scheme is instagram:

<dict>
    <key>CFBundleURLSchemes</key>
    <array>
        <string>instagram</string>
    </array>
</dict>

This is why you always get 'true', remove it, then try again with this:

if let instagramURL = URL(string: "instagram://app") {
    if UIApplication.shared.canOpenURL(instagramURL as URL) {
        print("instagram is installed")
    } else {
        print("instagram is not installed")
    }
}else{
    print("failed!")
}

ted

Maysam
  • 7,246
  • 13
  • 68
  • 106
  • Hi @Maysam, I have already implemented this code but always return true, even Instagram is not installed. print("instagram is installed") – Raj Joshi Dec 13 '18 at 10:36
  • 2
    You should not downvote an answer when you have not tried it, it's disrespectful to the community. I removed instagram on my phone and tried the code with the expected result: `instagram is not installed`. I have included a screenshot. – Maysam Dec 13 '18 at 11:19