我正在從事Xcode 7 beta 6。我試圖發送一個「刪除」請求http://mySubdomain.herokuapp.com應用交通安全Xcode 7 beta 6
我收到的錯誤是:
應用傳輸安全性已阻止明文HTTP(HTTP://)資源負載,因爲它是不安全的。臨時例外可以通過您的應用程序的Info.plist文件進行配置。
進行API調用時出錯:錯誤域= NSURLErrorDomain代碼= -1022由於App Transport安全策略要求使用安全連接,因此無法加載該資源。
NSLocalizedDescription =因爲應用傳輸安全策略要求使用安全連接的資源無法加載,NSUnderlyingError = {0x796f7ef0誤差區域= kCFErrorDomainCFNetwork代碼= -1022「(空)」}}
在我的實際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測試版6。
關於我選擇的答案 我選擇是正確的是適合我,因爲我改變了我的項目中的所有這些改變錯誤的plist文件並回答是,解決的可能性只有一個答案。其他答案提供的解決方案沒有錯,所以遇到此問題的其他人應該嘗試一下,因爲它們是有效的。我希望這可以幫助任何有類似問題的人。
指導,以查看網頁URL的可接受性可https://medium.com/@Mrugraj/app-transport-security-b7910c4fc70f。這可能有助於瞭解未來 – Mrug
首先嚐試在我的情況下工作任何方式,我覺得它有幫助,所以我有你的大拇指 –