pa1*_*a12 10 iphone nsurlconnection ios nsurlconnectiondelegate
我知道如果我使用以下nsurlconnectiondelegate它将被修复
- 连接:willSendRequestForAuthenticationChallenge: - 连接:canAuthenticateAgainstProtectionSpace
但我正在尝试使用
sendAsynchronousRequest:队列:completionHandler:
所以你没有得到回调.我调查了它所说的苹果文档
如果需要进行身份验证才能下载请求,则必须将所需凭据指定为URL的一部分.如果身份验证失败或缺少凭据,则连接将尝试在没有凭据的情况下继续.
我无法弄清楚如何做到这一点.当我抬起头时,我得到的是这个私人电话
+(void)setAllowsAnyHTTPSCertificate:(BOOL)inAllow forHost:(NSString*)inHost;
知道怎么做吗?
以下是我得到的错误
此服务器的证书无效.您可能正在连接到假装为"example.com = 0x8b34da0 {NSErrorFailingURLStringKey = https://example.com/test/,NSLocalizedRecoverySuggestion =您想要连接到服务器的服务器吗?",NSErrorFailingURLKey = https:/ /example.com/test/,NSLocalizedDescription =此服务器的证书无效.您可能正在连接到冒充"example.com"的服务器,这可能会使您的机密信息面临风险.,NSUnderlyingError = 0xa26c1c0"此服务器的证书无效.您可能正在连接到假装的服务器是"example.com",可能会使您的机密信息面临风险.",NSURLErrorFailingURLPeerTrustErrorKey =
您正在使用的Web服务器要求服务器信任身份验证,您需要使用适当的操作正确响应.您需要实现connection:willSendRequestForAuthenticationChallenge:委托方法并使用SecTrustRef对其进行身份验证.
更多信息可以在这里找到: - https://developer.apple.com/library/ios/technotes/tn2232/_index.html
这是我修复错误的代码:
- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
NSURLProtectionSpace *protectionSpace = [challenge protectionSpace];
id<NSURLAuthenticationChallengeSender> sender = [challenge sender];
if ([[protectionSpace authenticationMethod] isEqualToString:NSURLAuthenticationMethodServerTrust])
{
SecTrustRef trust = [[challenge protectionSpace] serverTrust];
NSURLCredential *credential = [[NSURLCredential alloc] initWithTrust:trust];
[sender useCredential:credential forAuthenticationChallenge:challenge];
}
else
{
[sender performDefaultHandlingForAuthenticationChallenge:challenge];
}
}
Run Code Online (Sandbox Code Playgroud)
尝试这个。
使用自定义会话配置启动会话,如下所示:
let session = URLSession(configuration: URLSessionConfiguration.ephemeral,
delegate: self,
delegateQueue: nil)
Run Code Online (Sandbox Code Playgroud)
实现以下委托回调方法:
public func urlSession(_: URLSession, task _: URLSessionTask, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
guard let serverTrust = challenge.protectionSpace.serverTrust else {
return completionHandler(URLSession.AuthChallengeDisposition.useCredential, nil)
}
return completionHandler(URLSession.AuthChallengeDisposition.useCredential, URLCredential(trust: serverTrust))
}
Run Code Online (Sandbox Code Playgroud)
你无法用你正在尝试的方式解决它
一切都不好。CFNetwork 目前对于苹果来说应该没问题,但其他 2 种方法甚至不是应用商店安全的
最好把服务器修好。这是最简单和最干净的
如果您正在使用AFNetworking,则可以使用以下代码:
(就像一个临时的客户端解决方案!)
AFHTTPSessionManager * apiManager = [AFHTTPSessionManager initWithBaseURL:[NSURL URLWithString:baseURL];
AFSecurityPolicy *policy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
policy.allowInvalidCertificates = YES;
apiManager.securityPolicy = policy;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
39665 次 |
| 最近记录: |