美文网首页
UIWebView加载HTTPS站点出现NSURLErrorDo

UIWebView加载HTTPS站点出现NSURLErrorDo

作者: LYPC_下里巴人 | 来源:发表于2018-06-18 16:43 被阅读107次

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];
    
}

相关文章

网友评论

      本文标题:UIWebView加载HTTPS站点出现NSURLErrorDo

      本文链接:https://www.haomeiwen.com/subject/mdmneftx.html