gpt4 book ai didi

iphone - 出于开发目的,如何使用 iOS 7 的 NSURLSession 及其委托(delegate)方法系列接受自签名 SSL 证书?

转载 作者:IT老高 更新时间:2023-10-28 11:30:26 25 4
gpt4 key购买 nike

我正在开发一个 iPhone 应用程序。 在开发过程中,我需要连接到使用自签名 SSL 证书的服务器。我很确定 - (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential *))completionHandler 是我写作的机会一些允许这样做的异常代码。但是,我找不到任何资源可以告诉我如何执行此操作。我可以在日志中看到以下错误:

NSURLConnection/CFURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9813)

除此之外,当我从上述委托(delegate)方法中 NSLog(@"error = %@", error); 时,我得到:

Error Domain=NSURLErrorDomain Code=-1202 "The certificate forthis server is invalid. You might be connecting to a server that ispretending to be api.mydevelopmenturl.example which could put yourconfidential information at risk." UserInfo=0x10cbdbcf0{NSUnderlyingError=0x112ec9730 "The certificate for this server isinvalid. You might be connecting to a server that is pretending to beapi.mydevelopmenturl.example which could put your confidential informationat risk.", NSErrorFailingURLStringKey=https://api.mydevelopmenturl.example/posts,NSErrorFailingURLKey=https://api.mydevelopmenturl.example/posts,NSLocalizedRecoverySuggestion=Would you like to connect to theserver anyway?, NSURLErrorFailingURLPeerTrustErrorKey=<SecTrustRef: 0x112e5a020>,NSLocalizedDescription=The certificate for this server is invalid.You might be connecting to a server that is pretending to beapi.mydevelopmenturl.example which could put your confidentialinformation at risk.}

关于如何解决此问题的任何想法?请发布代码,因为我已经阅读了概念文档,但我不理解它们。这是我无法理解的示例:https://developer.apple.com/library/content/technotes/tn2232/_index.html

最佳答案

这对我有用:

NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:Nil];
...
...
- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential *))completionHandler{
if([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]){
if([challenge.protectionSpace.host isEqualToString:@"mydomain.example"]){
NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
completionHandler(NSURLSessionAuthChallengeUseCredential,credential);
}
}
}

关于iphone - 出于开发目的,如何使用 iOS 7 的 NSURLSession 及其委托(delegate)方法系列接受自签名 SSL 证书?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19507207/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com