我的应用程序的执行通常会在 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);
}
}
}
最佳答案
每次调用此方法时都必须调用完成处理程序。您只是为一个保护空间调用它。
当操作系统在您的委托(delegate)上调用此方法时,NSURLSession
堆栈尽职尽责地等待您调用完成处理程序 block 。如果您未能调用完成处理程序,您的请求将处于不确定状态,直到请求超时。
要解决此问题,请在方法底部添加:
} else {
completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
}
关于ios - URLSession didReceiveChallenge 太慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42784810/