xcode7 iOS9 HTTP 不能正常使用的解决办法

分享 · xiaodu · 发布于 2 年前 · 4164 次阅读

升级Xcode7后发现之前写的http接口访问不了,查了发现 iOS9引入了新特性App Transport Security (ATS),新特性要求App内访问的网络必须使用HTTPS协议。详情:App Transport Security (ATS)

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.

##2种解决办法

  1. 升级服务端成Https
  2. 修改Info.plist

##Info.plist修改

1.在Info.plist中添加NSAppTransportSecurity类型Dictionary
2.在NSAppTransportSecurity下添加NSAllowsArbitraryLoads类型Boolean,值设为YES

###截图参考

1.在Filter中搜索Info.plist,选择Info.plist进行编辑

Fh_IC3nCoDewSVAx9oPPz4s1_IoL

2.按照上面提到的方式添加信息,正确的修改会看到下图这个样子,注意类型NSAppTransportSecurityDictionaryNSAllowsArbitraryLoadsBoolean,复制粘贴的时候,不要多了空格,segment fault 页面上直接复制,经常会多一个出空格!

Fn-6mpYKLSQjXvvAphRdnZFzl8uX

3.注意⚠️,单元测试下面也有一个Info.plist,修改那个文件是没有作用的!

当然还是建议还是服务端升级成Https,安全为主


###参考

1.http://segmentfault.com/a/1190000002933776
2.App Transport Security (ATS)

共收到 2 条回复
wangyongxin546 · #1 · 2 年前

模拟器无法加载http://图片,真机可以
更新完EI Capitan 10.11操作系统和XCODE Version 7.0.1 (7A1001) 后模拟器无法加载http://图片

@IBOutlet weak var iv: UIImageView!
@IBAction func test(sender: UIButton) {
    let url = "http://192.168.1.150:8081" + "/register/captcha.jhtml?captchaId="
    let nsd = NSData(contentsOfURL:NSURL(string: url)!)

    if nsd == nil {
        print("net error")
        return
    }
    let img = UIImage(data: nsd!)
    iv.image = img
}
suzee · #2 · 2 年前

mark一下

回帖