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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,559评论 4 61
  • IOS之UIWebView的使用 刚接触IOS开发1年多,现在对于 混合式 移动端开发越来越流行,因为开发成本上、...
    学无止境666阅读 45,959评论 5 53
  • 远远地就闻到了街口烤地瓜的香味,街灯已经亮了,月亮也升起来了,还有两块烤地瓜在炉灶上捂着,女孩还在固执地守在炉子旁...
    兰若9788阅读 3,114评论 0 2
  • 莉莉姐是我们公司的红人,但大家一直很奇怪,论身高,她算不上高挑;论样貌,也不算出众;平时,也不见她跟老板走得多近,...
    酷听听书阅读 2,507评论 0 0
  • 今天也是比较满意的一天,我完成了我必须完成的任务,嵌入式的两个作业都写完了,花了一天的时间,虽然中间也浪费了不少时...
    muziyue阅读 1,324评论 0 0

友情链接更多精彩内容