博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS UIWebView 加载自签名证书Https网页
阅读量:7099 次
发布时间:2019-06-28

本文共 4816 字,大约阅读时间需要 16 分钟。

  hot3.png

目前源码已经上传至Githup需要参考源码的可以点击这里:

使用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的设置与清除如下所示:

//设置CookieNSMutableDictionary *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 
@interface AKWebView : UIWebView+(instancetype)shareWebView;- (void)webViewWithLoadRequestWithURL:(NSURL *)url Fram:(CGRect)fram;@end

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

#import "AKWebView.h"@interface AKWebView ()
@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

你可能感兴趣的文章
Essential Studio for WPF 2018 v3最新版发布(上)
查看>>
Navicat使用教程:获取MySQL中的高级行数(第2部分)
查看>>
关于GDPR的六大理解
查看>>
cordova安卓全面屏适配
查看>>
总结2012展望2013
查看>>
Oracle入门必读
查看>>
Oracle实例和Oracle数据库
查看>>
MySQL群集,主从复制及双主模式
查看>>
SocketChannel / ServerSocketChannel / Selector
查看>>
grep 及正则表达式总结
查看>>
angularjs-currency 过滤器
查看>>
H3C-1000S 内部服务器映射
查看>>
Linux负载均衡软件LVS+keepalived
查看>>
世界500强某知名日企面试题库
查看>>
MySQL MyISAM 库转换为InnoDB的方法
查看>>
使用Python读Excel数据Insert到MySQL
查看>>
linux chkconfig and umask
查看>>
我的友情链接
查看>>
win2008安装win7主题
查看>>
ssh端口转发,先转载,最后自用心得
查看>>