我已经使用会话代理实现了自定义协议,如下所示 -
- (void)startLoading {
NSMutableURLRequest *mReq = [self.request mutableCopy];
NSURL *url = [[self request] URL];
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
...
...
if(!_mySession) {
_mySession = [NSURLSession sessionWithConfiguration:config
delegate:self
delegateQueue:[NSOperationQueue mainQueue]];
}
.....
}
- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge
completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler{
NSURLAuthenticationChallenge* challengeWrapper = [[NSURLAuthenticationChallenge alloc] initWithAuthenticationChallenge:challenge sender:[[CustomAuthChallengeWrappers alloc] initWithSessionCompletionHandler:completionHandler]];
if(self.client){
if([self.client respondsToSelector:@selector(URLProtocol:didReceiveAuthenticationChallenge:)]) {
debugLog("auth-challenge");
[self.client URLProtocol:self didReceiveAuthenticationChallenge:challengeWrapper];
}
}
}
如果客户端使用NSURLConnection,它工作正常。如果我在客户端使用NSURLSession,它会转发Auth质询但不会收回自定义协议。 挑战包装器按照以下链接实现: NSURLProtocol Challenge wrapper
我错过了NSURLSession的任何内容吗?
答案 0 :(得分:0)
只是为了更新它在iOS 10和xcode 8 beta版上运行良好。看起来iOS 9.3中存在一些问题,已在此版本中修复。