美文网首页常用的第三方牛叉的demo将来跳槽用
iOS 如何使用WebView加载Https类型的网页

iOS 如何使用WebView加载Https类型的网页

作者: 晓龙歌 | 来源:发表于2016-08-17 09:38 被阅读5430次

我现在做得这个项目里面包含了第三方支付,在使用的时候会有部分界面是调用网页来展示的,但是当我使用自带的WebView来加载这些网页的时候,就出错了:NSURLConnection/CFURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9813) 当时还不知道,后面在Mac下的浏览器打开就说什么是私密链接,而且网址是以Https开头的,百度了下,我这才知道原来是SLL没有设置。所以在加载Https的需要先用NSURLConnection来访问站点,然后在验证身份的时候,将该站点设置为可信任站点,最后用WebView重新加载一次就好了!

不多说了,贴代码:

//首先你得声明协议

//然后 你得声明三个全局变量

NSURLRequest*_originRequest;

NSURLConnection*_urlConnection;

BOOL_authenticated;

//再在 你需要用WebView加载网页的地方初始化Request

_originRequest= [NSURLRequestrequestWithURL:[NSURLURLWithString:urlStr]];

[self.allWebViewloadRequest:_originRequest];

//最后就是实现协议函数了

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType

{

NSLog(@"Did start loading: %@ auth:%d", [[requestURL]absoluteString],_authenticated);

if(!_authenticated) {

_authenticated=NO;

_urlConnection= [[NSURLConnectionalloc]initWithRequest:_originRequestdelegate:self];

[_urlConnectionstart];

returnNO;

}

returnYES;

}

-(void)webView:(UIWebView*)webView didFailLoadWithError:(NSError*)error{

// 102 == WebKitErrorFrameLoadInterruptedByPolicyChange

NSLog(@"***********error:%@,errorcode=%d,errormessage:%@",error.domain,error.code,error.description);

if(!([error.domainisEqualToString:@"WebKitErrorDomain"] && error.code==102)) {

//当请求出错了会做什么事情

}

}

#pragmamark-NURLConnectiondelegate

-(void)connection:(NSURLConnection*)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge*)challenge

{

NSLog(@"WebController Got auth challange via NSURLConnection");

if([challengepreviousFailureCount]==0)

{

_authenticated=YES;

NSURLCredential*credential=[NSURLCredentialcredentialForTrust:challenge.protectionSpace.serverTrust];

[challenge.senderuseCredential:credentialforAuthenticationChallenge:challenge];

}else

{

[[challengesender]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;

[self.allWebViewloadRequest:_originRequest];

// Cancel the URL connection otherwise we double up (webview + url connection, same url = no good!)

[_urlConnectioncancel];

}

// 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.authenticationMethodisEqualToString:NSURLAuthenticationMethodServerTrust];

}

//最后还需要一个NSURLRequest(NSURLRequestWithIgnoreSSL)的扩展,.m文件中只需要一个函数就好了。

+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString*)host

{

returnYES;

}

```

相关文章

网友评论

  • 简书弧线:太复杂了额, 用个简单的方法
    https://jingyan.baidu.com/article/454316ab672c02f7a6c03a63.html
    晓龙歌:@简书弧线 谢谢
  • Hengry:这样会请求两次哦,倒霉的是我的服务器不允许在同一时间内重复请求。后来我换了一种实现方式就解决了
    Hengry:@晓龙歌 http://www.jianshu.com/p/66135e477455
    晓龙歌:@DevHank 问下你怎么解决的呢?
  • 司马捷:一本正经的胡说八道
  • 会当凌绝顶丶:一本正经的胡说八道
  • 厚脸皮小强打不死的小强:allowsAnyHTTPSCertificateForHost私有api调用不会被拒绝吗
    晓龙歌:@厚脸皮小强打不死的小强 你在info.plist 这样配置
    <dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
    </dict>
    试试呢
    厚脸皮小强打不死的小强:这个私有api不就是信任https的任何证书吗?这个会不会被拒绝,跟后端支持https有啥关系,后端即使全部支持https,如果域名是采用的自签名证书的,那还是需要加这样的代码来绕过证书验证啊!苹果明确规定使用私有api会被拒绝,并且我才网上也看到使用allowsAnyHTTPSCertificateForHost私有api被拒的帖子,只不过是很久以前的!你的没被拒真的是见鬼了!苹果真操蛋啊.
    晓龙歌:@厚脸皮小强打不死的小强 现在是不会被拒绝的 但是等到2017年1月1日后就必须支持Https的请求了。那样后端就需要支持Https。
  • 践行者:这样写可以正常上线嘛?
    晓龙歌:@践行者 可以的 现在已经上线

本文标题:iOS 如何使用WebView加载Https类型的网页

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