我目前正在研究Xcode 7 beta 6.我正在尝试向http://mySubdomain.herokuapp.com发送"删除"请求
我收到的错误是:
App Transport Security已阻止明文HTTP(http://)资源加载,因为它不安全.可以通过应用程序的Info.plist文件配置临时例外.
进行API调用时出错:Error Domain = NSURLErrorDomain Code = -1022无法加载资源,因为App Transport Security策略要求使用安全连接.
NSLocalizedDescription =无法加载资源,因为App Transport Security策略需要使用安全连接.,NSUnderlyingError = 0x796f7ef0 {错误域= kCFErrorDomainCFNetwork代码= -1022"(null)"}}
在我的实际API调用中,我使用"https"而不是"http",这实际上适用于我的POST请求.但是DELETE请求会抛出上述错误.
我在这里看到了涉及pList文件的解决方案,但它们都没有对我有用.我在下面列出了我的尝试.
第一次尝试:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
Run Code Online (Sandbox Code Playgroud)
第二次尝试:
<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>
Run Code Online (Sandbox Code Playgroud)
最后,我甚至将所有这些临时密钥都放入:
<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>
Run Code Online (Sandbox Code Playgroud)
一切都没有运气!我总是得到同样的错误.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()
}
Run Code Online (Sandbox Code Playgroud)
再一次,我正在使用Xcode 7 beta 6.
关于 我选择的答案我选择的答案是正确的,因为我对项目中的错误pList文件进行了所有这些更改,而答案是唯一解决这种可能性的答案.其他答案提供的解决方案没有错,因此遇到此问题的任何其他人都应该尝试一下,因为它们是有效的.我希望这可以帮助任何有类似问题的人.
Man*_*Mal 46
我已经通过在info.plist中添加一些键来解决它.因为我使用目标C进行一些本机应用程序.
我遵循的步骤是:
打开我的Projects info.plist文件
增加了一个名为关键NSAppTransportSecurity的Dictionary.
NSAllowsArbitraryLoadsas 的子键Boolean,并将其值设置YES为如下图所示.清洁项目,现在一切正常,就像以前一样.
参考链接:
Car*_*ine 39
感谢您尝试将以下内容添加到plist文件中:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
Run Code Online (Sandbox Code Playgroud)
...你可能想尝试改变你的路线:
let url = NSURL(string:"https://mysubdomain.herokuapp.com/42kh24kh2kj2g24/clean")
Run Code Online (Sandbox Code Playgroud)
至:
let url = NSURL(string:"http://mysubdomain.herokuapp.com/42kh24kh2kj2g24/clean")
Run Code Online (Sandbox Code Playgroud)
如果您尝试过,请道歉.当你认为你已经筋疲力尽时,我能理解它是多么令人沮丧.
但是只要我在Xcode 7上运行我的应用程序,以便我可以测试我们的应用程序,就会启动"App Transport Security"问题.我们正在使用基于Oracle的Web服务,现在为时开始为基于SSL的HTTP配置数字证书已经太晚了.所以,上面添加到我的plist文件就可以了.感谢你说你试过这个.但是,只是为了帮助其他人,它确实对我有用.它需要我没有立即在我们的Oracle盒子上启用SSL的方法.
小智 13
在升级到xCode 7.0之后,我也无法覆盖App Transport Security,并尝试了相同类型的解决方案,但无济于事.离开它一段时间后,我注意到我在"MyAppName Tests"的支持文件下对Info.plist进行了更改,而不是项目本身的更改.我的项目中的Supporting Files文件夹没有展开,所以我甚至没有注意到它中的Info.plist.
典型的业余错误,我敢肯定,但它们在Project Navigator中只有几行,它让我很沮丧,直到我注意到这种区别.以为如果你遇到同样的问题,我会提到它.
小智 5
我已经解决为plist文件。
1.添加一个NSAppTransportSecurity : Dictionary。
2.添加子项命名NSAllowsArbitraryLoads为Boolean : YES
这很好。
在Xcode 8和Xcode 9中
AppTransportSecurityDictionary 的键.AllowsArbitraryLoadsBoolean 的子键,并将其值设置为YES,如下图所示.| 归档时间: |
|
| 查看次数: |
33224 次 |
| 最近记录: |