Xcode7文档下载以及一些新问题

在写完自己的第二个 App 之后,终于将 Xcode6升级到了 Xcode7.2(App store 提示更新7.2,结果安装后7.02,害的我从开发者中心里面重新下载了个),并开始 iOS9的适配,遇到一些 Xcode7新策略带来的问题,一一记录如下。

iOS9.2文档下载

从 Xcode 管理器里面下载文档太慢,好的办法就是找到下载链接用迅雷下载。 不同文档的资源链接都包含在这个 xml 里面了,链接是Apple Docsets,直接关键字查找对应文档的链接。

*iOS 9.2 Doc iOS9.2 Documentation

*OSX 10.11 Doc OS X 10.11 Documentation

*Xcode 7.2 Doc Xcode 7.2 Documentation

将这些 dmg 文件下载后运行安装,然后将安装解压出的文件拷贝至/Applications/Xcode.app/Contents/Developer/Documentation/DocSets

Http协议请求被禁

Xcode7下运行 App 发现网络请求失败,打印消息类似:

Application Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.  
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'

原来是iOS9的应用通讯安全策略进行了升级, 已不再支持 http 这种不安全的协议了,提倡 https 的连接方式。

App Transport Security

App Transport Security (ATS) enforces best practices in the secure connections between an app and its back end. ATS prevents accidental disclosure, provides secure default behavior, and is easy to adopt; it is also on by default in iOS 9 and OS X v10.11. You should adopt ATS as soon as possible, regardless of whether you’re creating a new app or updating an existing one.

If you’re developing a new app, you should use HTTPS exclusively. If you have an existing app, you should use HTTPS as much as you can right now, and create a plan for migrating the rest of your app as soon as possible. In addition, your communication through higher-level APIs needs to be encrypted using TLS version 1.2 with forward secrecy. If you try to make a connection that doesn't follow this requirement, an error is thrown. If your app needs to make a request to an insecure domain, you have to specify this domain in your app's Info.plist file.  

这虽是一种好的趋势,但对于大多数人来讲,立即全部转向 https 是不现实的,幸好,通过 Info.plist 的一些设置可以恢复 http 协议使用。

在 Info.plist 文件中,增加NSAppTransportSecurity的 Dictionary 类型 Item,同时在该项下增加NSAllowsArbitraryLoads的子 Item,类型 Bool,值设置 YES 即可。

Scheme 白名单

做社交分享时,发现微信、QQ应用无法跳转,原来在iOS 9下涉及到平台客户端跳转,系统会自动到项目info.plist下检测是否设置平台Scheme。对于需要配置的平台,如果没有配置,就无法正常跳转平台客户端。因此要支持客户端的分享和授权等,需要配置Scheme名单。

仍然是在 Info.plist 文件中添加类型为 Array 的 Item,属性名为LSApplicationQueriesSchemes,然后其子项目就是受信的 scheme,比如添加微信信任,在该项目下添加类型为 String 的子项,值设置为weixin

如果要一次性设置常见的应用scheme 白名单,可以将 Info.plist 文件以代码的形式打开,适当位置添加如下代码:

<key>LSApplicationQueriesSchemes</key>  
    <array>
        <string>mqqOpensdkSSoLogin</string>
        <string>mqzone</string>
        <string>sinaweibo</string>
        <string>alipayauth</string>
        <string>alipay</string>
        <string>safepay</string>
        <string>mqq</string>
        <string>mqqapi</string>
        <string>mqqopensdkapiV3</string>
        <string>mqqopensdkapiV2</string>
        <string>mqqapiwallet</string>
        <string>mqqwpa</string>
        <string>mqqbrowser</string>
        <string>wtloginmqq2</string>
        <string>weixin</string>
        <string>wechat</string>
    </array>