UIWebView加载https链接出现NSURLErrorDomain Code=-1202

今天后端同事给了我一个链接,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
    原文作者:Coffee_LaFa
    原文地址: https://www.jianshu.com/p/53c39533db35
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞