OGeek|极客世界-中国程序员成长平台

标题: ios - NSURLSession didReceiveChallenge 多个证书 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 22:54
标题: ios - NSURLSession didReceiveChallenge 多个证书

如何根据服务器指定多个证书? 在 session:didReceiveChallenge 方法中,我可以返回一个 NSURLCredential,但我没有找到一种方法来识别挑战来自哪个 URL。

我想做这样的事情:

-(void)URLSessionNSURLSession *)session didReceiveChallengeNSURLAuthenticationChallenge *)challenge completionHandlervoid (^)(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);
}



Best Answer-推荐答案


终于找到答案了,你可以像这样获取NSURLAuthenticationChallenge的宿主:

NSString* host = challenge.protectionSpace.host;

就是这样

关于ios - NSURLSession didReceiveChallenge 多个证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40819385/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4