iOS9 URL Schme 白名单

论坛 期权论坛 编程之家     
选择匿名的用户   2021-5-28 00:55   82   0

原文地址: https://github.com/ChenYilong/iOS9AdaptationTips

WWDC 2015 Session 703: "Privacy and Your App ( 时间在30:18左右)关于 URL scheme 的介绍,指出:

enter image description here

也就是说:在iOS9中,如果使用 canOpenURL: 方法,该方法所涉及到的 URL scheme 必须在"Info.plist"中将它们列为白名单,否则不能使用。key叫做LSApplicationQueriesSchemes ,键值内容是

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

  

白名单上限是50个:

WWDC 2015 Session 703: "Privacy and Your App )有说明:

“So for apps that are linked before iOS 9 and are running on iOS 9, they will be given 50 distinct URL schemes.” -- WWDC 2015 session 703 Privacy and Your App

iOS9中 openURL: 方法没有什么实质性的变化,仅仅多了一个确认动作:

enter image description here

苹果为什么要这么做?

在 iOS9 之前,你可以使用 canOpenURL: 监测用户手机里到底装没装微信,装没装微博。但是也有一些别有用心的 App ,这些 App 有一张常用 App 的 URL scheme,然后他们会多次调用canOpenURL: 遍历该表,来监测用户手机都装了什么 App ,比如这个用户装了叫“大姨妈”的App,你就可以知道这个用户是女性,你就可以只推给这个用户女性用品的广告。这是侵犯用户隐私的行为。

这也许就是原因。

本项目中给出了一个演示用的 Demo ,仓库的文件夹叫“Demo3_iOS9URLScheme适配_引入白名单概念”,Demo引用自LSApplicationQueriesSchemes-Working-Example

Demo结构如下:

enter image description here

主要演示的情景是这样的:

假设有两个App: weixin(微信) and 我的App. 我的App 想监测 weixin(微信) 是否被安装了. "weixin(微信)" 在 info.plist 中定义了 URL scheme :

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

  

我的App 想监测 weixin(微信) 是否被安装了 :

[[UIApplication sharedApplication]
                    canOpenURL:[NSURL URLWithString:@"weixin(微信)://"]];

  

即使你安装了微信,在iOS9中,这有可能会返回NO:

因为你需要将 "weixin(微信)" 添加到 “我的App” 的 info.plist 文件中:

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>weixin</string>
</array>

  

(以上只是为了演示,实际开发中,你不仅需要添加“weixin”还需要“wechat”这两个。具体下文给出表格)

另外,推荐一篇博文,其中最关键的是以下部分:

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;string>fbapi20150313</string> <string>fbapi20150629</string> <string>fbauth</string> <string>fbauth2</string> <string>fb-messenger-api20140430</string> </array>

  

如果使用 FBSDKMessengerShareKit,还要加上

<string>fb-messenger-platform-20150128</string>
<string>fb-messenger-platform-20150218</string>
<string>fb-messenger-platform-20150305</string>

  

如果使用SDK版本高于4.6,则只需要加上

<key>LSApplicationQueriesSchemes</key>
<array>
        <string>fbapi</string>
        <string>fb-messenger-api</string>
        <string>fbauth2</string>
        <string>fbshareextension</string>
</array>

  

参考链接

转载于:https://www.cnblogs.com/FranZhou/p/5190627.html

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP