美文网首页iOS项目
UIWebView加载https链接出现NSURLErrorDo

UIWebView加载https链接出现NSURLErrorDo

作者: 呆呆滴木木菇凉 | 来源:发表于2017-04-19 18:37 被阅读0次

    今天后端同事给了我一个链接,https请求的,证书是假的,就是在网页上打开显示链接不安全,让我看看能不能在ios客户端请求显示出来。用webview一加载,果然不行,报了一大段错误和详情。

    Error Domain=NSURLErrorDomain Code=-1202 "The certificate for this server is invalid.
    You might be connecting to a server that is pretending to be “h5.opencredit.com” which could put your confidential information at risk."
     UserInfo={NSURLErrorFailingURLPeerTrustErrorKey=<SecTrustRef: 0x6000001042f0>, NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, _kCFStreamErrorDomainKey=3, _kCFStreamErrorCodeKey=-9843
    

    尝试了下,去掉s改成http请求,完全可以。
    但是需要的还是https请求,故折腾了一下,找到方法,参考:http://stackoverflow.com/questions/11573164/uiwebview-to-view-self-signed-websites-no-private-api-not-nsurlconnection-i ,直接上代码。

    #import "ViewController.h"
    
    @interface ViewController ()<UIWebViewDelegate,NSURLConnectionDelegate>
    {
         BOOL _authenticated;
        NSURLConnection *_urlConnection;
        NSURLRequest *_request;
    }
    @property(nonatomic,strong)UIWebView *webview;
    @end
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        
        self.webview = [[UIWebView alloc]initWithFrame:self.view.bounds];
        self.webview.delegate = self;
       
        [self.view addSubview:self.webview];
        _request = [[NSURLRequest alloc]initWithURL:[NSURL URLWithString:url]];
         [self.webview loadRequest:_request];
        
    }
    
    -(void)webViewDidFinishLoad:(UIWebView *)webView
    {
        NSLog(@"did finish url = %@",webView.request.URL);
        
    }
    -(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
    {
        NSLog(@"error = %@",error.description);
    }
    -(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
    {
        if (!_authenticated) {
            _authenticated = NO;
            
            _urlConnection = [[NSURLConnection alloc] initWithRequest:_request delegate:self];
            
            [_urlConnection start];
            
            return NO;
        }
        return YES;
    }
    
    #pragma mark - NURLConnection delegate
    
    - (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];
    }
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    @end
    

    相关文章

      网友评论

        本文标题:UIWebView加载https链接出现NSURLErrorDo

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