2016-11-26 50 views
0

如何根據服務器指定多個證書? 在session:didReceiveChallenge方法中,我可以返回一個NSURLCredential,但我沒有找到一種方法來確定挑戰來自哪個URL。NSURLSession didReceiveChallenge多個證書

我想要做的做這樣的事情:

-(void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler 
{ 

    // get the certificate depending on the url 
    NSString *certificatePath; 
    if ([url isEqualToString: @"server1.com"]) { 
     certificatePath = [[[NSBundle mainBundle] bundlePath] stringByAppendingString:@"/server1.p12"]; 
    } else if ([url isEqualToString: @"server2.com"]) { 
     certificatePath = [[[NSBundle mainBundle] bundlePath] stringByAppendingString:@"/server2.p12"]; 
    } 

    //... some certificate stuff 

    NSURLCredential *credential = [NSURLCredential credentialWithIdentity:myIdentity certificates:(__bridge NSArray*)certsArray persistence:NSURLCredentialPersistencePermanent]; 
    completionHandler(NSURLSessionAuthChallengeUseCredential, credential); 
} 
+0

'如果(url === @「server1.com」)',你的意思是http://stackoverflow.com/questions/8239274/url-host-comparison? – Larme

+0

是的,這只是一個例子 –

回答

0

我終於找到了答案,就可以得到NSURLAuthenticationChallenge這樣的主機:

NSString* host = challenge.protectionSpace.host; 

就是這樣