UIWebView 加载https

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=, NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, _kCFStreamErrorDomainKey=3, _kCFStreamErrorCodeKey=-9813, NSErrorPeerCertificateChainKey=(
""
原因: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)