AshishVerma
AshishVerma

Reputation: 43

How do I open Instagram app from my iOS app?

I am trying to open the Instagram application from my iOS app through UIButton action but it's not working. It shows nothing.

Here is my code in Swift 3:

@IBAction func Instagram(_ sender: AnyObject) {
    let instagramURL = URL(string: "instagram://app")!
    if UIApplication.shared.canOpenURL(instagramURL) {
        UIApplication.shared.openURL(instagramURL)
    }

And here is my Plist Key -

Upvotes: 1

Views: 5533

Answers (2)

Sabrina
Sabrina

Reputation: 2799

For swift 4.2+ and ios 9+ This code launch Instagram, if it's not installed, launch the Instagram profile page in a web browser.

    let screenName =  "mehdico" // CHANGE THIS

    let appURL = URL(string:  "instagram://user?username=\(screenName)")
    let webURL = URL(string:  "https://instagram.com/\(screenName)")

    if UIApplication.shared.canOpenURL(appURL) {
        if #available(iOS 10.0, *) {
            UIApplication.shared.open(appURL, options: [:], completionHandler: nil)
        } else {
            UIApplication.shared.openURL(appURL)
        }
    } else {
        //redirect to safari because the user doesn't have Instagram
        if #available(iOS 10.0, *) {
            UIApplication.shared.open(webURL, options: [:], completionHandler: nil)
        } else {
            UIApplication.shared.openURL(webURL)
        }
    }

Upvotes: 5

Rajan Maheshwari
Rajan Maheshwari

Reputation: 14571

The method openUrl which you have posted in comments in deprecated too in iOS 10

Use this

@IBAction func openInstagramButtonPressed(_ sender: Any) {

    let instagram = URL(string: "instagram://app")!

    if UIApplication.shared.canOpenURL(instagram) {
            UIApplication.shared.open(instagram, options: ["":""], completionHandler: nil)
        } else {
            print("Instagram not installed")
    }
}

For the first time it will prompt you to open or cancel.

Also make sure to do the LSApplicationQueriesSchemes entry for instagram which you had already done.

Upvotes: 3

Related Questions