我試圖使用自簽名驗證調用WebAPI。我有殘疾ATS據我可以通過作用於下我的info.plist:如何在swift中調用具有自簽名ceritifcate的WebAPI
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
然而,即使添加此代碼後,我仍然收到了同樣的錯誤。代碼和錯誤如下。任何援助將不勝感激。
let postEndpoint: String = "https://api.rimorton.com/com.absolute.am.webapi/api/login"
let url = NSURL(string: postEndpoint)
let urlRequest = NSURLRequest(URL: url!)
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession(configuration: config)
let task = session.dataTaskWithRequest(urlRequest, completionHandler: {
(data, response, error) in
guard error == nil else {
print("error calling GET on /posts/1")
print(error)
return
}
guard let responseData = data else {
print("Error: did not receive data")
return
}
// parse the result as JSON, since that's what the API provides
let post: NSDictionary
do {
post = try NSJSONSerialization.JSONObjectWithData(responseData,
options: []) as! NSDictionary
} catch {
print("error trying to convert data to JSON")
return
}
// now we have the post, let's just print it to prove we can access it
print("The post is: " + post.description)
// the post object is a dictionary
// so we just access the title using the "title" key
// so check for a title and print it if we have one
if let postTitle = post["title"] as? String {
print("The title is: " + postTitle)
}
})
task.resume()
錯誤: 2016年2月28日19:40:03.521 LANrevTarget_Dev [19173:878737] NSURLSession/NSURLConnection的HTTP加載失敗(kCFStreamErrorDomainSSL,-9813) 錯誤調用GET上/帖/ 1 可選(錯誤域= NSURLErrorDomain代碼= -1202「此服務器的證書無效,您可能正在連接假裝爲」api.rimorton.com「的服務器,這可能會將您的機密信息置於危險之中。」UserInfo = {NSURLErrorFailingURLPeerTrustErrorKey =,NSLocalizedRecoverySuggestion =你想連接服務器嗎?,_kCFStreamErrorDomainKey = 3,_kCFStreamErrorCodeKey = -9813,NSErrorPeerCertificateChainKey = {type = immutable,count = 1,values =( 0: )},NSUnderlyingError = 0x7fb78b542e00 {錯誤域= kCFErrorDomainCFNetwork代碼= -1202 「(空)」 的UserInfo = {_ kCFStreamPropertySSLClientCertificateState = 0,kCFStreamPropertySSLPeerTrust =,_kCFNetworkCFStreamSSLErrorOriginalValue = -9813,_kCFStreamErrorDomainKey = 3,_kCFStreamErrorCodeKey = -9813,kCFStreamPropertySSLPeerCertificates = {type = immutable,count = 1,values =( 0: )}}},NSLocalizedDescription =此服務器的證書無效。您可能正在連接到一個僞裝成「api.rimorton.com」,它可以把你的機密信息的安全服務器,NSErrorFailingURLKey = https://api.rimorton.com/com.absolute.am.webapi/api/login,NSErrorFailingURLStringKey = https://api.rimorton.com/com.absolute.am.webapi/api/login,NSErrorClientCertificateStateKey = 0})