2017-03-14 101 views
1

我的應用程序的執行通常在didReceiveChallenge停止2-3秒(甚至5秒)。大約十分之一的時間需要永遠。 整件事情有效,但我能做些什麼來加速它?URLSession didReceiveChallenge太慢

這裏是我的代碼:

- (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); 
     } 
    } 
} 

回答

0

必須調用完成處理每到這個方法被調用。你只是稱它爲單個保護空間。

當操作系統在您的代理上調用此方法時,NSURLSession堆棧會在那裏盡職盡責地等待您調用完成處理程序塊。如果你沒有打電話給完成處理程序,你的請求將會被擱置,直到請求超時。

爲了解決這個問題,在該方法的底部,添加:

} else { 
    completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil); 
}