【问题标题】:Instagram share opening another app if Instagram is not installed in my device如果我的设备中未安装 Instagram,则 Instagram 共享打开另一个应用程序
【发布时间】:2019-05-14 12:03:30
【问题描述】:

我正在尝试在 Instagram 上分享图片,如果我的设备上安装了 Instagram,它可以正常工作,否则它将打开另一个与 Instagram 分享功能相同的应用程序(而不是抛出错误)。

我的 Instagram 分享代码是:

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

我在Info.plist

中使用过CFBundleURLSchemesLSApplicationQueriesSchemes方案
 <dict>
    <key>CFBundleURLSchemes</key>
    <array>
        <string>instagram</string>
    </array>
</dict>

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

有人对此有解决方案吗?未安装 Instagram 时,我无法将图像发布到 Instagram,我什至无法检测是否安装了 Instagram。

遵循来自 https://www.instagram.com/developer/mobile-sharing/iphone-hooks/ 的自定义 URL 方案

【问题讨论】:

  • 未安装 instagram 时打开哪个应用?
  • canOpenURL: 不会检查已安装的应用程序,它只会告诉您它是否可以在 Safari 中或通过应用程序打开该 URL。有效的 URL 将始终返回 true,因为系统实际上可以在某个地方打开它。
  • 如果任何其他应用程序实现与 instagram 相同的自定义 url 架构,那么它是可能的
  • 嗨@SPatel,我最近在三个应用程序中实现了这一点,如果没有安装 instagram 应用程序,这两个应用程序都会相互打开。
  • @ricardopereira,请查看这里instagram.com/developer/mobile-sharing/iphone-hooks

标签: ios swift instagram socialshare


【解决方案1】:

首先,你需要了解Info.plist!你告诉编译你的应用方案是instagram

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

这就是为什么你总是得到“真”,删除它,然后再试一次:

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

【讨论】:

  • 嗨@Maysam,我已经实现了这段代码但总是返回true,即使没有安装Instagram。 print("instagram 已安装")
  • 你不应该在没有尝试过的时候投反对票,这是对社区的不尊重。我删除了手机上的 Instagram 并尝试了预期结果的代码:instagram is not installed。我已经附上了截图。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-02-18
  • 1970-01-01
  • 1970-01-01
  • 2015-10-13
  • 1970-01-01
  • 1970-01-01
  • 2019-04-15
相关资源
最近更新 更多