无法通过重定向[iOS 9]在UIWebView中加载HTTPS页面
我正在尝试在iOS 9+中使用无法通过重定向[iOS 9]在UIWebView中加载HTTPS页面,ios,objective-c,ssl,uiwebview,nsurlconnection,Ios,Objective C,Ssl,Uiwebview,Nsurlconnection,我正在尝试在iOS 9+中使用UIWebView加载一个https://网页 它给出了以下错误: NSURLSession/NSURLConnection HTTP加载失败 (kCFStreamErrorDomainSSL,-9802)CFNetwork SSLHandshake失败(-9802) 我已经尝试过使用NSURLConnection的各种方法,首先使用connection:willSendRequestForAuthenticationChallenge:方法处理身份验证挑战 虽然这
UIWebView
加载一个https://网页
它给出了以下错误:
NSURLSession/NSURLConnection HTTP加载失败
(kCFStreamErrorDomainSSL,-9802)CFNetwork SSLHandshake失败(-9802)
我已经尝试过使用NSURLConnection
的各种方法,首先使用connection:willSendRequestForAuthenticationChallenge:
方法处理身份验证挑战
虽然这对于直接请求很有效,但是(实际问题开始了)对于重定向到另一个具有不同域的页面的网页,这不起作用
例如,最初我加载UIWebView
,并调用https://example.com
然后此页面重定向到https://someothersecuredomain.com
。这是因为连接失败并且未调用连接:willSendRequestForAuthenticationChallenge:
以下是我的实施:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// Load webview with request url and parameters
NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPBody:postBody];
[request setHTTPMethod: @"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
self.webView.delegate = self;
[self.webView loadRequest: request];
_BaseRequestURL = url;
}
#pragma mark UIWebView delegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if (![[request.URL absoluteString] isEqualToString:[_FailedRequest.URL absoluteString]]) {
_FailedRequest = request;
(void)[[NSURLConnection alloc] initWithRequest:request delegate:self];
return NO;
}
return YES;
}
#pragma mark - NSURLConnectionDataDelegate methods
-(void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
NSURL *baseURL = [_FailedRequest URL];
if ([challenge.protectionSpace.host isEqualToString:baseURL.host]) {
NSLog(@"trusting connection to host %@", challenge.protectionSpace.host);
[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
} else
NSLog(@"Not trusting connection to host %@", challenge.protectionSpace.host);
}
[challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)pResponse {
[connection cancel];
[self.webView loadRequest:_FailedRequest];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"connection error : %@", error);
}
下面是记录的连接错误:
Error Domain=NSURLErrorDomain Code=-1200“发生SSL错误
无法与服务器建立安全连接。”
UserInfo={NSUnderlyingError=0x138643250{Error
Domain=kCFErrorDomainCFNetwork Code=-1200“发生SSL错误
无法与服务器建立安全连接。”
UserInfo={NSErrorFailingURLStringKey=,,
nsLocalizedRecoverysSuggestion=是否要连接到服务器
无论如何?,KCFnetworkCfsStreamsLerrorOriginalValue=-9802,
_kCFStreamPropertySSLClientCertificateState=0,NSLocalizedDescription=发生SSL错误,并且存在安全漏洞
无法与服务器建立连接。,kCFStreamErrorDomainKey=3,
NSErrorFailingURLKey=,
_kCFStreamErrorCodeKey=-9802},NSErrorFailingURLStringKey=,,
NSErrorFailingURLKey=,
nsLocalizedRecoverysSuggestion=是否要连接到服务器
Anywhere?,NSLocalizedDescription=发生SSL错误,并且发生了安全错误
无法连接到服务器。}
根据iOS 9的应用程序传输安全,我已经将这两个域添加到我的Info.plist
中的异常域列表中
这是这样的:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<false/>
<key>NSExceptionDomains</key>
<dict>
<key>example.com</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSTemporaryExceptionMinimumTLSVersion</key>
<string>TLSv1.1</string>
</dict>
<key>secure.ccavenue.com</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSTemporaryExceptionMinimumTLSVersion</key>
<string>TLSv1.1</string>
</dict>
</dict>
</dict>
NSAppTransportSecurity
NSAllowsArbitraryLoads
NSExceptionDomains
example.com
n包括多个域
NSTemporary ExceptionalLowsInSecureHttpLoads
NSTemporaryExceptionMinimumTLSVersion
TLSv1.1
secure.ccavenue.com
n包括多个域
NSTemporary ExceptionalLowsInSecureHttpLoads
NSTemporaryExceptionMinimumTLSVersion
TLSv1.1
我真的陷入了困境,任何帮助都将不胜感激
PS.已经尝试过这些问题:、、和您是否尝试过使用真实值的NSAllowsArbitraryLoads?尝试过,相同的错误。还有其他方法可以让它工作吗?你解决问题了吗,@UditS?@new2ios:没有,我们无法找到解决方案,最终不得不更改应用程序流程。10x用于回复,@UditS