我正在研究 Xcode 7 beta 6 . 我正在尝试向http://mySubdomain.herokuapp.com发送"DELETE"请求
我收到的错误是:
App Transport Security已阻止明文HTTP(http://)资源加载,因为它不安全 . 可以通过应用程序的Info.plist文件配置临时例外 . 进行API调用时出错:Error Domain = NSURLErrorDomain Code = -1022无法加载资源,因为App Transport Security策略要求使用安全连接 . NSLocalizedDescription =无法加载资源,因为App Transport Security策略需要使用安全连接 . ,NSUnderlyingError = 0x796f7ef0 }
在我的实际API调用中,我输入“https”而不是“http”,这实际上适用于我的POST请求 . 但是DELETE请求会抛出上述错误 .
我在这里看到了涉及pList文件的解决方案,但它们都没有对我有用 . 我在下面列出了我的尝试 .
第一次尝试:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
第二次尝试:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>herokuapp.com</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSExceptionRequiresForwardSecrecy</key>
<false/>
<key>NSExceptionMinimumTLSVersion</key>
<string>TLSv1.2</string>
<key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
<false/>
<key>NSThirdPartyExceptionMinimumTLSVersion</key>
<string>TLSv1.2</string>
<key>NSRequiresCertificateTransparency</key>
<false/>
</dict>
</dict>
</dict>
最后,我甚至将所有这些临时密钥都放入:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>herokuapp.com</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSTemporaryIncludesSubdomains</key>
<true/>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSExceptionRequiresForwardSecrecy</key>
<false/>
<key>NSTemporaryExceptionRequiresForwardSecrecy</key>
<false/>
<key>NSExceptionMinimumTLSVersion</key>
<string>TLSv1.2</string>
<key>NSTemporaryExceptionMinimumTLSVersion</key>
<string>TLSv1.2</string>
<key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSTemporaryThirdPartyExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
<false/>
<key>NSTemporaryThirdPartyExceptionRequiresForwardSecrecy</key>
<false/>
<key>NSThirdPartyExceptionMinimumTLSVersion</key>
<string>TLSv1.2</string>
<key>NSTemporaryThirdPartyExceptionMinimumTLSVersion</key>
<string>TLSv1.2</string>
<key>NSRequiresCertificateTransparency</key>
<false/>
<key>NSTemporaryRequiresCertificateTransparency</key>
<false/>
</dict>
</dict>
</dict>
一切都没有运气!我总是得到同样的错误 . DELETE请求格式正确,因为当我从Postman手动执行时,我得到了所需的结果 .
这是我的实际API调用方法的样子,以防万一可能存在问题:
class func makeDELETEALLRequest(completion: (error:Bool) -> Void) {
let session = NSURLSession.sharedSession()
let url = NSURL(string:"https://mysubdomain.herokuapp.com/42kh24kh2kj2g24/clean")
let request = NSMutableURLRequest(URL: url!)
request.HTTPMethod = "DELETE"
let task = session.dataTaskWithRequest(request) { (data, response, error) -> Void in
if (error != nil) {
print("Error making API call: \(error!)")
completion(error: true)
} else {
let HTTPResponse = response as! NSHTTPURLResponse
let statusCode = HTTPResponse.statusCode
if (statusCode == 200){
print("Successfully deleted!")
completion(error: false)
} else {
print("Different status code: \(statusCode)")
completion(error: true)
}
}
}
task.resume()
}
再一次,我正在使用 Xcode 7 beta 6 .
ABOUT MY SELECTED ANSWER 我选择的答案是正确的,因为我对项目中的错误pList文件进行了所有这些更改,而答案是唯一解决这种可能性的答案 . 其他答案提供的解决方案没有错,因此遇到此问题的任何其他人都应该尝试一下,因为它们是有效的 . 我希望这可以帮助任何有类似问题的人 .
5 回答
在升级到xCode 7.0之后,我也无法覆盖App Transport Security,并尝试使用相同类型的解决方案,但无济于事 . 离开它一段时间后,我注意到我在“MyAppName Tests”的支持文件下对Info.plist进行了更改,而不是项目本身的更改 . 我的项目中的Supporting Files文件夹没有展开,所以我甚至没有注意到它中的Info.plist .
典型的业余错误,我敢肯定,但它们在Project Navigator中只有几行,它让我感到沮丧,直到我注意到这种区别 . 我想如果你遇到同样的问题,我会提到它 .
我通过在info.plist中添加一些键来解决它 . 因为我正在使用目标C进行一些本机应用程序 .
我遵循的步骤是:
打开我的Projects
info.plist
文件添加了一个名为
NSAppTransportSecurity
的密钥作为Dictionary
.添加了一个名为
NSAllowsArbitraryLoads
的子键为Boolean
,并将其值设置为YES
,如下图所示 .清理项目,现在一切正常,就像以前一样 .
参考链接:
https://stackoverflow.com/a/32631185/2905967
https://stackoverflow.com/a/32609970
感谢您尝试将以下内容添加到plist文件中:
...你可能想尝试改变你的路线:
至:
如果您尝试过此,请道歉 . 当你认为你已经筋疲力尽时,我能理解它是多么令人沮丧 .
但是,只要我在Xcode 7上运行我的应用程序,以便我可以测试我们的应用程序,就会启动“App Transport Security”问题 . 我们正在使用基于Oracle的Web服务,现在为时尚太晚,无法开始为基于SSL的HTTP配置数字证书 . 所以,上面添加到我的plist文件就可以了 . 感谢你说你试过这个 . 但是,只是为了帮助其他人,它确实对我有用 . 它需要我没有立即在我们的Oracle盒子上启用SSL的方法 .
在Xcode 8和Xcode 9中
打开我的Projects info.plist文件
添加名为
AppTransportSecurity
的键作为词典 .添加一个名为
AllowsArbitraryLoads
的子键作为布尔值,并将其值设置为YES,如下图所示 .我已解决为plist文件 .
1.添加
NSAppTransportSecurity : Dictionary
.2.添加名为
NSAllowsArbitraryLoads
的子项为Boolean : YES
这很好用 .