NSURLConnection 实现webView显示HTTPS页面

  • 我们在浏览器访问https页面的时候的,会弹出:
    信任证书

  • 我们接下来信任证书以及显示出来

遵循协议

@interface ViewController ()<NSURLConnectionDataDelegate>

interface:

@interface ViewController ()<NSURLConnectionDataDelegate>
/**
 -  存储data数据
 */
@property(nonatomic,strong)NSMutableData *dataM;
/**
 -  访问url链接
 */
@property(nonatomic,strong)NSURL *url;
@property(nonatomic,weak)IBOutlet UIWebView *webView;
@end

viewDidLoad:创建url以及发送请求

- (void)viewDidLoad {
    [super viewDidLoad];

    self.url = [NSURL URLWithString:@"https://mail.com"];
    NSURLRequest *request = [NSURLRequest requestWithURL:self.url];
    //发送请求
    [NSURLConnection connectionWithRequest:request delegate:self];

}

实现代理方法:

#pragma mark - NSURLSessionDataDelegate代理方法
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge
 completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * __nullable credential))completionHandler
{
    NSLog(@"challenge = %@",challenge.protectionSpace.serverTrust);
    //判断是否是信任服务器证书
    if (challenge.protectionSpace.authenticationMethod ==NSURLAuthenticationMethodServerTrust)
    {
        //创建一个凭据对象
        NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
        //告诉服务器客户端信任证书
        [challenge.sender useCredential:credential forAuthenticationChallenge:challenge];

    }
}

/**
 *  接收到服务器返回的数据时调用
 */
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    NSLog(@"接收到的数据%zd",data.length);
    [self.dataM appendData:data];
}
/**
 *  请求完毕
 */
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSString *html = [[NSString alloc]initWithData:self.dataM encoding:NSUTF8StringEncoding];

    NSLog(@"请求完毕");
    [self.webView loadHTMLString:html baseURL:self.url];
}

懒加载:

- (NSMutableData *)dataM
{
    if (_dataM == nil)
    {
        _dataM = [[NSMutableData alloc]init];
    }
    return _dataM;
}

至此,我们已经实现了https数据的展示:

注意:

  • NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9843) 原因:没有信任服务器证书

在下面这个方法中:

 - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge
 completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * __nullable credential))completionHandler()

我们通过protectionSpace.authenticationMethod判断是否信任服务器证书
- NSURLSessionAuthChallengeUseCredential = 0, 使用凭据 ,信任服务器证书
- NSURLSessionAuthChallengePerformDefaultHandling = 1, 默认处理,忽略服务器证书
- NSURLSessionAuthChallengeCancelAuthenticationChallenge = 2, 整个请求被取消 凭据被忽略
- NSURLSessionAuthChallengeRejectProtectionSpace = 3, 本次拒绝,下次重试

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值