目前源码已经上传至Githup须要参考源码的能够点击这里:https://github.com/AustinKuture/UIWebViewSelfSignedHttps.gitgit
使用UIWebView 加载页面时,咱们的经常使用方法大都是这样的(或者使用WKWebView):github
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的设置与清除以下所示:web
//设置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的实现方法:cookie
#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:ui
@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:atom
#pragma mark ***UIWebView 加载方法*** - (void)webViewWithLoadRequestWithURL:(NSURL *)url Fram:(CGRect)fram{ self.frame = fram; self.delegate = self; _requestW = [NSURLRequest requestWithURL:url]; [self loadRequest:_requestW]; }
5,实现各代理方法:url
#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大小便可:spa
AKWebView *webView = [AKWebView shareWebView]; [webView webViewWithLoadRequestWithURL:url Fram:CGRectMake(0, 0, 500, 500)]; [self.view addSubview:webView];
"既专一技术也享受生活--iOS资源库"
QQ群:524955567
天天一篇最有价值的文章
微信公众号:YiKun_info
微信公众号: