1

我的应用程序的执行通常在 didReceiveChallenge 停止 2-3 秒(甚至 5 秒)。大约十分之一的时间需要永远。整个事情都有效,但我能做些什么来加快它呢?

这是我的代码:

- (void)URLSession:(NSURLSession *)session
didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge
 completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * __nullable credential))completionHandler{
    NSLog(@"*** KBRequest.NSURLSessionDelegate - didReceiveChallenge IOS10");
    if([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]){
        if([challenge.protectionSpace.host isEqualToString:@"engine.my.server"]){
            NSURLCredential *credential = [NSURLCredential credentialForTrust: challenge.protectionSpace.serverTrust];
            completionHandler(NSURLSessionAuthChallengeUseCredential,credential);
        }
        else{
            completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge, nil);
        }
    }
}
4

1 回答 1

1

每次调用此方法时,您都必须调用完成处理程序。您只是为了一个保护空间而调用它。

当操作系统在您的委托上调用此方法时,NSURLSession堆栈会尽职尽责地等待您调用完成处理程序块。如果您未能调用完成处理程序,您的请求将一直处于等待状态,直到请求超时。

要解决此问题,请在方法的底部添加:

} else {
    completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
}
于 2017-07-11T18:18:29.710 回答