iOS UIWebView 加载自签名证书Https网页

目前源码已经上传至Githup需要参考源码的可以点击这里:https://github.com/AustinKuture/UIWebViewSelfSignedHttps.git

使用UIWebView 加载页面时,我们的常用方法大都是这样的(或者使用WKWebView):

UIWebView *webView = [UIWebView alloc]initWithFrame:CGRectMake(0,0,200,200);
[webView loadRequest:[NSURLRequest requestWithURL:url]];
[self.view addSubView:webView];

有时我们也会用UIWe不View加载网页时设置Cookie,在这里我们使用NSHTTPCookieStorage实现管理共享的cookie,并将存储一个共享实例,这些Cookie在所有应用程序之间是共享的,并且是跨进程同步保存的,通过对Cookie设置可以让我们像电脑浏览网页一样,灵活的对Cookie信息进行设置和清除,使应用端加载网页信息时更加的方便,Cookie的设置与清除如下所示:

//设置Cookie
NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary];  
[cookieProperties setObject:@"cookie_user" forKey:NSHTTPCookieName];  
[cookieProperties setObject:uid forKey:NSHTTPCookieValue];  
[cookieProperties setObject:@"xxx.xxx.com" forKey:NSHTTPCookieDomain];
[cookieProperties setObject:@"/" forKey:NSHTTPCookiePath];  
[cookieProperties setObject:@"0" forKey:NSHTTPCookieVersion]; 
[cookieProperties setObject:[[NSDate date] dateByAddingTimeInterval:2629743] forKey:NSHTTPCookieExpires];  

NSHTTPCookie *cookieuser = [NSHTTPCookie cookieProperties];  
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookieuser]; 

 NSURL *url = [NSURL URLWithString:url];   
 NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:60];  
 [webView loadRequest:request];

//清除Cookie
- (void)deleteCookie{  
    NSHTTPCookie *cookie;  

    NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];  

    NSArray *cookieAry = [cookieJar cookiesForURL: [NSURL URLWithString: url]];  

    for (cookie in cookieAry) {  

        [cookieJar deleteCookie: cookie];  

    }  
}

虽然UIWebView使用较为灵活,几乎与电脑端加载网页一样的方便,但是由于种种原因iOS的UIWebView在使用自签名证书加载网页时仍然不能正常的加载数据并显示,对此我们可以使用NSURLConnection和UIWebView的代理方法,实现对自签名证书在一定情况下,自动在程序后台进行验证与自动信任,从而加载出我们所想要显示的网页,具体使用步骤如下:

1,创建一个继承自UIWebView的类AKWebView,在.h文件中添加两个方法,一个是初始化类方法(单例),另一个是WebView的实现方法:

#import <UIKit/UIKit.h>

@interface AKWebView : UIWebView

+(instancetype)shareWebView;
- (void)webViewWithLoadRequestWithURL:(NSURL *)url Fram:(CGRect)fram;

@end

2,在.m文件中,首先添加我们所要用到的代理协议,并创建属性:

#import "AKWebView.h"

@interface AKWebView ()<UIWebViewDelegate,NSURLConnectionDataDelegate>

@property (nonatomic,strong) NSURLConnection *urlConnection;
@property (nonatomic,strong) NSURLRequest *requestW;
@property (nonatomic) SSLAuthenticate authenticated;

@end

3,实现类初始化方法shareWebView:

@implementation AKWebView

+(instancetype)shareWebView{
    static dispatch_once_t onece = 0;
    static AKWebView *webView = nil;
    dispatch_once(&onece, ^(void){
        webView = [[self alloc]init];
    });
    return webView;
}

4,实现webView的实例化方法,在这里主要是为了配置WebVeiw的大小,布局,添加代理,以及Request:

#pragma mark ***UIWebView 加载方法***
- (void)webViewWithLoadRequestWithURL:(NSURL *)url Fram:(CGRect)fram{
    
    self.frame = fram;
    self.delegate = self;
    _requestW = [NSURLRequest requestWithURL:url];
    [self loadRequest:_requestW];
}

5,实现各代理方法:

#pragma mark ***UIWebView 代理方法***
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    
    NSLog(@"开始加载: %@ 授权:%d", [[request URL] absoluteString], _authenticated);
    
    if (!_authenticated) {
        _authenticated = kNeverAuthenticate;
        
        _urlConnection = [[NSURLConnection alloc] initWithRequest:_requestW delegate:self];
        
        [_urlConnection start];
        
        return NO;
    }
    
    return YES;
}


#pragma mark ***NURLConnection 代理方法***
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{
    NSLog(@"WebController 已经得到授权正在请求 NSURLConnection");
    
    if ([challenge previousFailureCount] == 0){
        _authenticated = kTryAuthenticate;
        
        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 已经收到响应并通过了 NSURLConnection请求");
    
    _authenticated = kTryAuthenticate;
    [self loadRequest:_requestW];
    [_urlConnection cancel];
}

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace{
    
    return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}




@end

6,使用方法,在使用时候先导入AKWebView的头文件,在想要加载自签名证书https网页的控制器中,创建AKwebView并实现其方法,添加URL,及Fram大小即可:

AKWebView *webView = [AKWebView shareWebView];
[webView webViewWithLoadRequestWithURL:url Fram:CGRectMake(0, 0, 500, 500)];
[self.view addSubview:webView];

 

 

 

"既专注技术也享受生活--iOS资源库"

QQ群:524955567

 

每天一篇最有价值的文章

微信公众号:YiKun_info

微信公众号:

转载于:https://my.oschina.net/Kuture/blog/812783

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值