iOS 9 未使用 URL 方案打开 Instagram 应用

iOS 9 not opening Instagram app with URL SCHEME

以下 URL 在 iOS 8.3 及更低版本上打开,但它不起作用并且 iOS 9

let instagramURL = NSURL(string: "instagram://app")

为什么 URL 打不开?

这是 iOS 9 的新安全功能。观看 WWDC 2015 Session 703 了解更多信息。

任何使用 SDK 9 构建的应用程序都需要在其 plist 文件中提供一个 LSApplicationQueriesSchemes 条目,声明它尝试查询的方案。

<key>LSApplicationQueriesSchemes</key>
<array>
 <string>urlscheme</string>
 <string>urlscheme2</string>
 <string>urlscheme3</string>
 <string>urlscheme4</string>
</array> 

iOS 9 对URL方案的处理做了一个小改动。您必须将 url 列入白名单,您的应用将使用 Info.plist.

中的 LSApplicationQueriesSchemes 键调出该 url

请在此处查看 post:http://awkwardhare.com/post/121196006730/quick-take-on-ios-9-url-scheme-changes

主要结论是:

If you call the “canOpenURL” method on a URL that is not in your whitelist, it will return “NO”, even if there is an app installed that has registered to handle this scheme. A “This app is not allowed to query for scheme xxx” syslog entry will appear.

If you call the “openURL” method on a URL that is not in your whitelist, it will fail silently. A “This app is not allowed to query for scheme xxx” syslog entry will appear.

作者还推测这是 OS 的一个错误,Apple 将在后续版本中修复此问题。

假设有两个应用 TestA 和 TestB。 TestB 想查询是否安装了 TestA。 "TestA" 在其 info.plist 文件中定义了以下 URL 方案:

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>testA</string>
        </array>
    </dict>
</array>

第二个应用程序 "TestB" 尝试通过调用来确定 "TestA" 是否已安装:

[[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"TestA://"]];

但这在 iOS9 中通常 return 否,因为 "TestA" 需要添加到 TestB 的 info.plist 文件中的 LSApplicationQueriesSchemes 条目中。这是通过将以下代码添加到 TestB 的 info.plist 文件来完成的:

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>TestA</string>
</array>

可在此处找到有效的实现: https://github.com/gatzsche/LSApplicationQueriesSchemes-Working-Example

似乎没有人解决如何使用嵌入式参数指定 URL 的问题。任何包含参数的 URL 都不可能在 LSApplicationsQueriesSchemes 中指定特定的 URL。例如,假设我有一个传递发件人电子邮件地址的电子邮件应用程序:

myemail://mailto?bob@gmail.com

似乎让它在 iOS9 中工作的唯一方法是删除任何参数。

来自,Session 703 WWDC 2015:

You can continue to use URL schemes when you build your app for iOS 9 and you want to call URL schemes, you will now need to declare them in your apps Info.plist. There is a new key, LSApplicationQueriesSchemes, and here you will need to add the list of schemes you want to are canOpenURL on.

如上所述,您想在信息 plist 中添加一个密钥,这里是大多数社交网络的列表

<key>LSApplicationQueriesSchemes</key>
    <array>
     <string>fb</string>
     <string>fbapi</string>
     <string>fbauth2</string>
     <string>fbshareextension</string>
     <string>fb-messenger-api</string>
     <string>twitter</string>
     <string>viber</string>
     <string>whatsapp</string>
     <string>wechat</string>
     <string>line</string>
     <string>instagram</string>
     <string> instagram-stories</string>
     <string>kakaotalk</string>
     <string>mqq</string>
     <string>vk</string>
     <string>comgooglemaps</string>
     <string>googlephotos</string>
     <string>ha</string>
     <string>yammer</string>
    </array>

* 前3个匹配Facebook(FBSDK 4.6):fbapi、fbauth2、fbshareextension。 "Ha" 用于 snapchat

即使使用@Matthieu 的回答(这对于其他社交 URLs 来说 100% 正确),从共享对话框进行的 Facebook 共享也会失败。我必须添加一组 URL 我从 Facebook SDK 中撤消的

<array>
        <string>fbapi</string>
        <string>fbauth2</string>
        <string>fbshareextension</string>
        <string>fb-messenger-api</string>
        <string>twitter</string>
        <string>whatsapp</string>
        <string>wechat</string>
        <string>line</string>
        <string>instagram</string>
        <string>kakaotalk</string>
        <string>mqq</string>
        <string>vk</string>
        <string>comgooglemaps</string>
        <string>fbapi20130214</string>                                                    
        <string>fbapi20130410</string>                                                     
        <string>fbapi20130702</string>                                                    
        <string>fbapi20131010</string>                                                    
        <string>fbapi20131219</string>                                                    
        <string>fbapi20140410</string>                                                     
        <string>fbapi20140116</string>                                                     
        <string>fbapi20150313</string>                                                     
        <string>fbapi20150629</string>
    </array>

重要的是要注意 9.0.x 上的越狱手机存在错误,该错误破坏了 url 方案。如果您 运行 是越狱设备,请确保在 Cydia

中更新 Patcyh

您可以直接调用 openURL:openURL:options:completionHandler:(iOS 10 起)打开应用程序,而无需进行条件检查 canOpenURL:。 请阅读 Apple doc for canOpenURL: method 中的 讨论部分 ,其中写道:

the openURL: method is not constrained by the LSApplicationQueriesSchemes requirement.

当我今天尝试从我自己的应用程序调用 Facebook 时,我发现没有可以添加到 Info.plistLSApplicationQueriesSchemes 键(Xcode 版本 8.2.1 (8C1002))。我用 Sublime Text 打开 Info.plist 并手动将其添加到文件中,然后就可以了。只是想让你知道,如果你找不到密钥,只需自己添加即可。

Apple 在 iOS 9 上更改了 canOpenURL 方法。在 iOS 9 和 iOS 10 上检查 URL 方案的应用程序必须在提交给 Apple 时声明这些方案。

对于 PayPal 添加以下 URL 方案:

参考这个link

Swift3.1,Swift3.2,Swift4

if let urlFromStr = URL(string: "instagram://app") {
    if UIApplication.shared.canOpenURL(urlFromStr) {
        if #available(iOS 10.0, *) {
            UIApplication.shared.open(urlFromStr, options: [:], completionHandler: nil)
        } else {
            UIApplication.shared.openURL(urlFromStr)
        }
    }
}

在Info.plist中添加这些:

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

在 Swift 4.2 和 Xcode 10.1

在info.plist中需要添加CFBundleURLSchemes和LSApplicationQueriesSchemes。

--> Select info.plist 在你的项目中,

--> 右击,

--> Select 打开方式 源代码

请参阅下面的屏幕截图

--> xml 文件将被打开

--> 复制-粘贴下面的代码并替换为你的 ID

用于 gmail、fb、twitter 和链接的 CFBundleURLSchemes 和 LSApplicationQueriesSchemes。

<key>CFBundleURLTypes</key>
    <array>
    <dict>
        <key>CFBundleTypeRole</key>
        <string>Editor</string>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>li5****5</string>
            <string>com.googleusercontent.apps.8***************5f</string>
            <string>fb8***********3</string>
            <string>twitterkit-s***************w</string>
        </array>
    </dict>
</array>
<key>FacebookAppID</key>
<string>8*********3</string>
<key>FacebookDisplayName</key>
<string>K************ app</string>
<key>LSApplicationQueriesSchemes</key>
<array>
    <string>fbapi</string>
    <string>fb-messenger-share-api</string>
    <string>fbauth2</string>
    <string>fbshareextension</string>

    <string>twitter</string>
    <string>twitterauth</string>

    <string>linkedin</string>
    <string>linkedin-sdk2</string>
    <string>linkedin-sdk</string>

</array>

请参阅下面的屏幕,您的最终 info.plist 文件是