UIWebView加载HTTPS站点出现NSURLErrorDomain code=-1202
遵守协议:
@interface ViewController () <UIWebViewDelegate, NSURLConnectionDelegate>
{
// 创建属性
UIWebView *WebView;
NSString *UrlString;
NSURLConnection *_urlConnection;
NSURLRequest *_request;
BOOL _authenticated;
}
// 加载连接
- (void)viewDidLoad {
[super viewDidLoad];
WebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, kStatusBarHeight, mScreenWidth, mScreenHeight-kStatusBarHeight)];
WebView.delegate = self;
_request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://shuangchuang.chinacoal.mobi:8081"]];
[WebView loadRequest:_request];
[self.view addSubview:WebView];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSLog(@"加载完毕");
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
{
NSLog(@"Did start loading: %@ auth:%d", [[request URL] absoluteString],_authenticated);
if (!_authenticated) {
_authenticated =NO;
_urlConnection = [[NSURLConnection alloc] initWithRequest:_request delegate:self];
[_urlConnection start];
return NO;
}
return YES;
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
// 102 == WebKitErrorFrameLoadInterruptedByPolicyChange
NSLog(@"***********error:%@,errorcode=%ld,errormessage:%@",error.domain,(long)error.code,error.description);
if (!([error.domain isEqualToString:@"WebKitErrorDomain"] && error.code ==102)) {
//[self dismissWithError:error animated:YES];
}
}
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;
{
NSLog(@"WebController Got auth challange via NSURLConnection");
if ([challenge previousFailureCount] ==0)
{
_authenticated =YES;
NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
[challenge.sender useCredential:credential forAuthenticationChallenge:challenge];
} else
{
[[challenge sender] cancelAuthenticationChallenge:challenge];
}
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
{
NSLog(@"WebController received response via NSURLConnection");
// remake a webview call now that authentication has passed ok.
_authenticated =YES;
[WebView loadRequest:_request];
// Cancel the URL connection otherwise we double up (webview + url connection, same url = no good!)
[_urlConnection cancel];
}
// We use this method is to accept an untrusted site which unfortunately we need to do, as our PVM servers are self signed.
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}








网友评论