如何从我的iOS应用程序中打开Instagram应用程序?

问题描述 投票:0回答:2

我试图通过UIButton操作从我的iOS应用程序打开Instagram应用程序,但它无法正常工作。它没有任何表现。

这是我在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 -

ios swift instagram
2个回答
3
投票

您在评论中发布的方法openUrl在iOS 10中也已弃用

用这个

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

这是第一次提示您打开或取消。

还要确保为你已经完成的instagram的LSApplicationQueriesSchemes条目。


1
投票

对于swift 4.2和ios 9+此代码启动instagram,如果未安装,请在Web浏览器中启动instagram配置文件页面。

    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)
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.