查找completionHandler

时间:2015-09-14 04:41:57

标签: swift closures swift2 alamofire

我想在Swift 2.0 Alamofire库中调试SSL问题。我使用自定义Alamofire Manager和自定义ServerTrustPolicy设置如下:

static let manager: Manager = {
    let serverTrustPolicies: [String: ServerTrustPolicy] = [
        "localhost": .PinCertificates(
            certificates: ServerTrustPolicy.certificatesInBundle(),
            validateCertificateChain: false,
            validateHost: false
        )
//        "localhost": .DisableEvaluation
    ]

    let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
    configuration.HTTPAdditionalHeaders = Alamofire.Manager.defaultHTTPHeaders

   return Alamofire.Manager(configuration: configuration,
    serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies))
}()

我同时尝试了.PinCertificates.DisableEvaluation。两个都给我带来了同样的错误

Error Domain=NSURLErrorDomain Code=-1200 "An SSL error has occurred and a secure connection to the server cannot be made."
UserInfo={
  NSURLErrorFailingURLPeerTrustErrorKey=<SecTrustRef: 0x60c00004d980>,
  NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?,
  _kCFStreamErrorDomainKey=3, 
  _kCFStreamErrorCodeKey=-9802, 
  NSErrorPeerCertificateChainKey=<CFArray 0x6060001498a0 [0x10bb3c7b0]>{
    type = immutable, count = 1, values = (
     0 : <cert(0x61600006ed80) s: localhost i: localhost>
    )}, 
   NSUnderlyingError=0x6040000ad750 {
     Error Domain=kCFErrorDomainCFNetwork Code=-1200 "(null)"
     UserInfo={
       _kCFStreamPropertySSLClientCertificateState=0, 
       kCFStreamPropertySSLPeerTrust=<SecTrustRef: 0x60c00004d980>,
       _kCFNetworkCFStreamSSLErrorOriginalValue=-9802,
       _kCFStreamErrorDomainKey=3,
       _kCFStreamErrorCodeKey=-9802,
       kCFStreamPropertySSLPeerCertificates=<CFArray 0x6060001498a0 [0x10bb3c7b0]>{
         type = immutable, count = 1, values = (
           0 : <cert(0x61600006ed80) s: localhost i: localhost>
         )}
     }
   },
   NSLocalizedDescription=An SSL error has occurred and a secure connection to the server cannot be made.,
   NSErrorFailingURLKey=https://localhost:3000/auth/requestToken?auth_appId=d018ccd505db2cb1d5aacabb03fc2f3a,
   NSErrorFailingURLStringKey=https://localhost:3000/auth/requestToken?auth_appId=d018ccd505db2cb1d5aacabb03fc2f3a,
   NSErrorClientCertificateStateKey=0
}

我尝试使用curl做的请求很好

curl --cacert ./ca.pem https://localhost:3000

我把它缩小到一个方法,从中调用completionHandler。在关闭被调用之前,一切都很好,所以我想知道这个completionHandler实际上做了什么。

public func URLSession(
    session: NSURLSession,
    didReceiveChallenge challenge: NSURLAuthenticationChallenge,
    completionHandler: ((NSURLSessionAuthChallengeDisposition, NSURLCredential?) -> Void))
{
    // some other stuff but
    // everything fine so far

    completionHandler(disposition, credential)
}

我认为URLSession方法是从调度队列或类似的东西调用的。

所以我的问题:如何找到作为completionHandler方法的URLSession参数传递的闭包?

0 个答案:

没有答案