最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]Swift中使用SwiftHTTP去POST出错: App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure

Swift crifan 2331浏览 0评论
[背景]
尝试在Swift中去执行HTTP的POST,用的是库:
daltoniam/SwiftHTTP · GitHub
代码如下:
        let params = ["phone": "\(nameTF.text)", "password": "\(psdTF.text)"]
        NSLog("params = \(params)")
        NSLog("LOGIN_URL=\(LOGIN_URL)")

        do {
            let opt = try HTTP.POST(LOGIN_URL, parameters: params, requestSerializer: JSONParameterSerializer())
            //opt.onFinish = { response in
            opt.start { response in
                if let err = response.error {
                    print("error: \(err.localizedDescription)")
                    return //also notify app of failure as needed
                }
                print("opt finished: \(response.description)")
            }
        } catch let error {
            NSLog("got an error creating the request: \(error)")

        }
然后出错:
2015-10-19 13:56:48.229 [6694:1225161] App 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.
error: The resource could not be loaded because the App Transport Security policy requires the use of a secure connection.
如图:
App Transport Security has blocked a cleartext HTTP http resource load since it is insecure
[解决过程]
1.搜:
SwiftHTTP POST  App 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.
参考:
“To be clear, developers should only consider disabling ATS if other approaches to comply with ATS standards are unsuccessful. Apple has provided a tech note describing different approaches, including the ability to selectively enable ATS for a list of provided HTTPS sites.”
去看看info.plist:
add info list new row
设置为Dictionary
set to dictionary for item in info plist
再去添加子项:
add icon to new item
注意要 先点击 左边的变成下拉箭头 再点击右边的加号,添加子项
然后设置类型为Boolean,值为YES
NSAllowArbitraryLoads boolean yes
然后再去运行看看结果
然后就可以继续执行了:
(此处post出错,和本问题无关,表示post操作正常执行了)
http post return 200 code
后来又遇到一次,再去设置:
jiandao app set http allow
然后可以查看原始值:
right click show raw key values
效果是:
show info plist raw keys and values
即:
变成前面说的:NSAllowsArbitraryLoads了。
[总结]
此处HTTP的POST出现警告后,去info.plist中:
右击
-》Add Row
-》键值输入:NSAppTransportSecurity
-》设置类型为Dictionary
-》点击NSAppTransportSecurity左边按钮变成下拉状态
-》点击右边的加号去新家一个子项
-》名为:NSAllowsArbitraryLoads
-》设置NSAllowsArbitraryLoads的值为Boolean类型,值为YES
即可。
之后的HTTP的POST即可正常执行了。
关于相关内容的解释,详见官网:

转载请注明:在路上 » [已解决]Swift中使用SwiftHTTP去POST出错: App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
86 queries in 0.153 seconds, using 22.12MB memory