NSURLSession.sharedSession()で取得したsessionオブジェクトのdataTaskWithURLメソッドが失敗した時に吐き出されるエラーは、こんな感じです。(xcode7 beta + Swift2.0)
- NSErrorを取り出すコードの断片
foo.swift
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithURL(url,
completionHandler: { data, response, error in
if error != nil {
print("err descpription = \(error!.description)")
print("err = \(error!.localizedDescription)")
print("err code = \(error!.code)")
print("err domain = \(error!.domain)")
print("err userInfo = \(error!.userInfo)")
}
- debug consoleの出力
foo.swift
2015-06-23 13:49:10.814 xxx[4869:202756] CFNetwork SSLHandshake failed (-9824)
2015-06-23 13:49:10.827 xxx[4869:202756] NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9824)
err descpription = Error Domain=NSURLErrorDomain Code=-1200 "An SSL error has occurred and a secure connection to the server cannot be made." UserInfo=0x7fe20be44500 {_kCFStreamErrorCodeKey=-9824, NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, NSUnderlyingError=0x7fe20be3eec0 "The operation couldn’t be completed. (kCFErrorDomainCFNetwork error -1200.)", NSLocalizedDescription=An SSL error has occurred and a secure connection to the server cannot be made., NSErrorFailingURLKey=https://www.apple.com/, NSErrorFailingURLStringKey=https://www.apple.com/, _kCFStreamErrorDomainKey=3}
err = An SSL error has occurred and a secure connection to the server cannot be made.
err code = -1200
err domain = NSURLErrorDomain
err userInfo = [_kCFStreamErrorCodeKey: -9824, NSLocalizedRecoverySuggestion: Would you like to connect to the server anyway?, NSUnderlyingError: Error Domain=kCFErrorDomainCFNetwork Code=-1200 "The operation couldn’t be completed. (kCFErrorDomainCFNetwork error -1200.)" UserInfo=0x7fe20be3ec00 {_kCFStreamPropertySSLClientCertificateState=0, _kCFNetworkCFStreamSSLErrorOriginalValue=-9824, _kCFStreamErrorDomainKey=3, _kCFStreamErrorCodeKey=-9824}, NSLocalizedDescription: An SSL error has occurred and a secure connection to the server cannot be made., NSErrorFailingURLKey: https://www.apple.com/, NSErrorFailingURLStringKey: https://www.apple.com/, _kCFStreamErrorDomainKey: 3]
このあたりは、Swift2のバージョンが上がるに従いthrowsへ書き直される予感!