美文网首页
UIWebView 加载https

UIWebView 加载https

作者: 墨凌风起 | 来源:发表于2019-06-04 11:39 被阅读0次

UIWebView 加载HTTPS 有时会触发
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
NSLog(@"error = %@",error);

}
error = Error Domain=NSURLErrorDomain Code=-1202 "The certificate for this server is invalid. You might be connecting to a server that is pretending to be “123.xx.xx.4” which could put your confidential information at risk." UserInfo={NSURLErrorFailingURLPeerTrustErrorKey=<SecTrustRef: 0x174138420>, NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, _kCFStreamErrorDomainKey=3, _kCFStreamErrorCodeKey=-9813, NSErrorPeerCertificateChainKey=(
"<cert(0x1030a6a00) s: 192.168.1.29 i: Trustmobi CA>"
原因:https ssl证书不受信任
解决办法:UIWebView 加载时忽略证书

-(void)createWebView{
       self.tishiWeb = [[UIWebView alloc]init];
      _tishiWeb.delegate=self;
      NSString *encodedString = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
      NSURL *url = [NSURL URLWithString:encodedString];
      _request = [NSURLRequest requestWithURL:url];
      [NSURLConnection connectionWithRequest:_request delegate:self];
      [self.view addSubview:_tishiWeb];
}

- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{
        if ([challenge previousFailureCount]== 0) {
                NSURLCredential* cre = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
                [challenge.sender useCredential:cre forAuthenticationChallenge:challenge];
            }
}


- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
        [self.tishiWeb loadRequest:_request];
        [connection cancel];
}

相关文章

网友评论

      本文标题:UIWebView 加载https

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