ios - URLSession didReceiveChallenge 太慢

标签 ios authentication https nsurlsession nsurlcredential

我的应用程序的执行通常会在 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/

相关文章:

performance - 强制整个站点使用 HTTPS 的好习惯还是坏习惯?

ios - 调用电话操作无法快速工作

ios - 将自定义对象存储为 NSDictionary 与 NSData

authentication - spring-security-oauth2 JwkTokenStore 与自定义用户详细信息服务

python - 自定义用户身份验证。它是如何完成的,有最佳实践吗?

java - Spring 安全。删除一个拦截 URL 后不重定向到登录页面

ios - 如何调整 UITableView tableHeaderView 的左右 insets 或 margins?

ios - Sharekit 试飞和 pod

ssl - 证书颁发机构签署 CSR 时的安全隐患

tomcat - 将 HTTP 转换为 HTTPs