IOS10下的ATS问题

最近IOS10更新之后,我的《微打卡》APP在调用微博API接口的时候,一直失败,提示以下错误:

NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9802)

大部分的开发者都知道从IOS9以后,apple默认禁止非https的网络访问,所以我们的通用做法是在info.plist里增加

NSAppTransportSecurity
 
NSAllowsArbitraryLoads 


但是最新的wwdc的会上,苹果又放出新规:

从2017年1月1日起,,所有新提交的 app 默认不允许使用NSAllowsArbitraryLoads
来绕过ATS的限制,默认情况下你的 app 可以访问加密足够强的(TLS V1.2以上)HTTPS内容;

针对这个问题,我们可以使用NSExceptionDomains对特定域名开放http请求
,比如第三方微博登录,我们可以进行如下配置:

NSAppTransportSecurity
 
  NSExceptionDomains
  
   sina.cn
   
    NSThirdPartyExceptionMinimumTLSVersion
    TLSv1.0
    NSThirdPartyExceptionRequiresForwardSecrecy
    
    NSIncludesSubdomains
    
   
   weibo.cn
   
    NSThirdPartyExceptionMinimumTLSVersion
    TLSv1.0
    NSThirdPartyExceptionRequiresForwardSecrecy
    
    NSIncludesSubdomains
    
   
   weibo. com
   
    NSThirdPartyExceptionMinimumTLSVersion
    TLSv1.0
    NSThirdPartyExceptionRequiresForwardSecrecy
    
    NSIncludesSubdomains
    
   
   sinaimg.cn
   
    NSThirdPartyExceptionMinimumTLSVersion
    TLSv1.0
    NSThirdPartyExceptionRequiresForwardSecrecy
    
    NSIncludesSubdomains
    
   
   sinajs.cn
   
    NSThirdPartyExceptionMinimumTLSVersion
    TLSv1.0
    NSThirdPartyExceptionRequiresForwardSecrecy
    
    NSIncludesSubdomains
    
   
   sina.com.cn
   
    NSThirdPartyExceptionMinimumTLSVersion
    TLSv1.0
    NSThirdPartyExceptionRequiresForwardSecrecy
    
    NSIncludesSubdomains
    
   
  
 

不过,如果有条件还是建议将你的网站升级到https,毕竟这也是未来的趋势。

你可能感兴趣的:(IOS10下的ATS问题)