【问题标题】:How do I open Instagram app from my iOS app?如何从我的 iOS 应用打开 Instagram 应用?
【发布时间】:2020-05-07 02:00:26
【问题描述】:

我正在尝试通过 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)
    }

【问题讨论】:

  • 请将您的实际代码复制并粘贴到您的问题中。您发布的代码甚至无法编译。
  • 并确保包含实际尝试打开 Instagram 应用的代码,因为您发布的代码不会尝试实际打开应用。
  • 这是我用来打开 Instagram 应用程序的全部代码!
  • 1) 该代码无法编译,所以不,这不是您的实际代码。 2) 该代码不会尝试打开 Instagram。
  • 你能给我打开Instagram应用程序的代码吗?

标签: ios swift instagram


【解决方案1】:

您在 cmets 中发布的方法 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 条目。

【讨论】:

  • 不,它不起作用,因为点击按钮后没有任何动作。!
  • 它的工作。检查您的按钮插座。在按钮内保留一个调试器
  • 哇,现在工作代码也被否决了,希望 SO 应该做一个功能来上传一个简短的执行视频。
  • 使用已弃用的openURL 不是问题。
  • @rmaddy 哦,这就是投反对票的原因,太棒了
【解决方案2】:

适用于 swift 4.2+ 和 ios 9+ 此代码启动 Instagram,如果未安装,请在网络浏览器中启动 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)
        }
    }

【讨论】:

  • 选项和 NSURL 的使用不一致,而不是简单的 URL,这是怎么回事?
  • @Clashsoft 感谢您的来信。代码已更新。
猜你喜欢
  • 2017-02-10
  • 2015-10-13
  • 1970-01-01
  • 1970-01-01
  • 2014-12-04
  • 1970-01-01
  • 2015-11-03
相关资源
最近更新 更多