142

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

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

为什么网址打不开?

4

14 回答 14

213

iOS 9 对 URL 方案的处理做了一个小改动。您必须将您的应用程序将使用LSApplicationQueriesSchemes您的Info.plist.

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

主要结论是:

如果您对不在白名单中的 URL 调用“canOpenURL”方法,它将返回“NO”,即使安装了已注册处理此方案的应用程序也是如此。将出现“此应用不允许查询方案 xxx”的系统日志条目。

如果您对不在白名单中的 URL 调用“openURL”方法,它将静默失败。将出现“此应用不允许查询方案 xxx”的系统日志条目。

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

于 2015-06-22T19:34:25.327 回答
168

如上所述,您想在 info 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。“哈”是用于snapchat

于 2015-09-07T14:58:03.093 回答
87

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

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

<key>LSApplicationQueriesSchemes</key>
<array>
 <string>urlscheme</string>
 <string>urlscheme2</string>
 <string>urlscheme3</string>
 <string>urlscheme4</string>
</array> 
于 2015-06-22T19:33:07.430 回答
25

Assuming two apps TestA and TestB. TestB wants to query if TestA is installed. "TestA" defines the following URL scheme in its info.plist file:

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

The second app "TestB" tries to find out if "TestA" is installed by calling:

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

But this will normally return NO in iOS9 because "TestA" needs to be added to the LSApplicationQueriesSchemes entry in TestB's info.plist file. This is done by adding the following code to TestB's info.plist file:

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

A working implementation can be found here: https://github.com/gatzsche/LSApplicationQueriesSchemes-Working-Example

于 2015-06-25T10:34:13.280 回答
23

来自 2015 年 WWDC 703 会议:

当您为 iOS 9 构建应用程序时,您可以继续使用 URL 方案并且您想要调用 URL 方案,您现在需要在您的应用程序 Info.plist 中声明它们。有一个新键 ,LSApplicationQueriesSchemes您需要在此处添加您想要使用 canOpenURL 的方案列表。

例如,在 FB 的情况下

于 2015-08-14T09:02:38.937 回答
4

即使有@Matthieu 的回答(对于其余的社交 URL,这是 100% 正确的),来自共享对话框的 Facebook 共享也会失败。我必须添加一组从 Facebook SDK 反转的 URL。

<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>
于 2015-10-12T11:03:35.267 回答
2

在 Swift 4.2 和 Xcode 10.1 中

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

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

--> 右键,

--> 选择作为源代码打开

请参阅下面的屏幕截图

在此处输入图像描述

--> 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文件是

在此处输入图像描述

于 2019-02-14T07:00:56.113 回答
1

需要注意的是,9.0.x 上的越狱手机存在一个错误,它破坏了 url 方案。如果您运行的是越狱设备,请确保在 Cydia 中更新 Patcyh

于 2015-10-27T22:03:05.713 回答
1

openURL:好吧,您可以通过调用或openURL:options:completionHandler:(iOS 10 及更高版本)直接打开应用程序,而无需进行条件检查canOpenURL:。请阅读Apple 文档中关于 canOpenURL: 方法的讨论部分,其中说:

openURL: 方法不受LSApplicationQueriesSchemes要求的限制。

于 2017-02-09T09:13:03.597 回答
0

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

myemail://mailto?bob@gmail.com

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

于 2015-07-24T20:34:26.007 回答
0

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

于 2017-06-14T09:18:09.283 回答
0

对于 PayPal,请添加以下 URL 方案:

在此处输入图像描述

参考这个链接

于 2017-08-28T11:46:05.733 回答
0

斯威夫特 3.1、斯威夫特 3.2、斯威夫特 4

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>
于 2017-09-23T18:28:42.260 回答
-1

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

于 2017-02-27T22:10:47.750 回答