【问题标题】:Find declaration of completionHandler查找completionHandler的声明
【发布时间】:2015-12-10 00:26:49
【问题描述】:

我想在 Swift 2.0 Alamofire 库中调试 SSL 问题。我正在使用带有自定义 ServerTrustPolicy 设置的自定义 Alamofire 管理器,如下所示:

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 方法的闭包?

【问题讨论】:

  • 你应该可以进入闭包。或者只是检查这个方法在哪里被调用。
  • 你的问题不是很清楚。您是否使用自定义ServerTrustPolicy? Alamofire 默认使用 Apple 的默认服务器信任评估。如果你能更具体一点,帮助会容易得多。
  • 我用更多信息更新了我的问题 :)

标签: swift closures swift2 alamofire


【解决方案1】:

- 我认为您的问题不是这种特殊方法,而是completionHandlerSwift 中的工作方式。

- 这里是completionHandler 中的closure,而Callback 又是Callback

-Swift 中,回调由closuredelegates 处理。

-让我用一个小例子来消除混淆,

问题:

假设我有一个要调用的方法,所以我将调用该方法并在获得响应后,我需要在屏幕或控制台上显示结果。

解决方案:

  • 要调用的方法:

internal func sumUp(valOne: Int, valTwo: Int, completionHandler: (sum: Int)-> Void) { let add = valOne + valTwo completionHandler(sum: add) }

  • 调用代码:

sumUp{

      (sum) -> Void in
       print(sum) // OR print it in display

}

- 调用方法sumUp 将完成对两个整数求和的工作,然后返回总和completionHandler,将其暴露给caller .

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-24
    • 1970-01-01
    • 1970-01-01
    • 2015-02-03
    • 1970-01-01
    • 1970-01-01
    • 2011-08-24
    • 1970-01-01
    相关资源
    最近更新 更多