UIWebView
自iOS2就有,WKWebView
从iOS8才有,毫无疑问WKWebView
将逐步取代笨重的UIWebView
。经过简单的测试便可发现UIWebView
占用过多内存,且内存峰值更是夸张。WKWebView
网页加载速度也有提高,可是并不像内存那样提高那么多。下面列举一些其它的优点:javascript
estimatedProgress
UIWebView
使用很是简单,能够分为三步,也是最简单的用法,显示网页css
- (void)simpleExampleTest { // 1.建立webview,并设置大小,"20"为状态栏高度 UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height - 20)]; // 2.建立请求 NSMutableURLRequest *request =[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.cnblogs.com/mddblog/"]]; // 3.加载网页 [webView loadRequest:request]; // 最后将webView添加到界面 [self.view addSubview:webView]; }
- (void)loadRequest:(NSURLRequest *)request; - (void)loadHTMLString:(NSString *)string baseURL:(nullable NSURL *)baseURL; - (void)loadData:(NSData *)data MIMEType:(NSString *)MIMEType textEncodingName:(NSString *)textEncodingName baseURL:(NSURL *)baseURL;
UIWebView
不只能够加载HTML页面,还支持pdf、word、txt、各类图片等等的显示。下面以加载mac桌面上的png图片:/Users/coohua/Desktop/bigIcon.png
为例html
// 1.获取url NSURL *url = [NSURL fileURLWithPath:@"/Users/coohua/Desktop/bigIcon.png"]; // 2.建立请求 NSURLRequest *request=[NSURLRequest requestWithURL:url]; // 3.加载请求 [self.webView loadRequest:request];
// 刷新 - (void)reload; // 中止加载 - (void)stopLoading; // 后退函数 - (void)goBack; // 前进函数 - (void)goForward; // 是否能够后退 @property (nonatomic, readonly, getter=canGoBack) BOOL canGoBack; // 是否能够向前 @property (nonatomic, readonly, getter=canGoForward) BOOL canGoForward; // 是否正在加载 @property (nonatomic, readonly, getter=isLoading) BOOL loading;
一共有四个方法java
/// 是否容许加载网页,也可获取js要打开的url,经过截取此url可与js交互 - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { NSString *urlString = [[request URL] absoluteString]; urlString = [urlString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSArray *urlComps = [urlString componentsSeparatedByString:@"://"]; NSLog(@"urlString=%@---urlComps=%@",urlString,urlComps); return YES; } /// 开始加载网页 - (void)webViewDidStartLoad:(UIWebView *)webView { NSURLRequest *request = webView.request; NSLog(@"webViewDidStartLoad-url=%@--%@",[request URL],[request HTTPBody]); } /// 网页加载完成 - (void)webViewDidFinishLoad:(UIWebView *)webView { NSURLRequest *request = webView.request; NSURL *url = [request URL]; if ([url.path isEqualToString:@"/normal.html"]) { NSLog(@"isEqualToString"); } NSLog(@"webViewDidFinishLoad-url=%@--%@",[request URL],[request HTTPBody]); NSLog(@"%@",[self.webView stringByEvaluatingJavaScriptFromString:@"document.title"]); } /// 网页加载错误 - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { NSURLRequest *request = webView.request; NSLog(@"didFailLoadWithError-url=%@--%@",[request URL],[request HTTPBody]); }
主要有两方面:js执行OC代码、oc调取写好的js代码git
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
函数。// 实现自动定位js代码, htmlLocationID为定位的位置(由js开发人员给出),实现自动定位代码,应该在网页加载完成以后再调用 NSString *javascriptStr = [NSString stringWithFormat:@"window.location.href = '#%@'",htmlLocationID]; // webview执行代码 [self.webView stringByEvaluatingJavaScriptFromString:javascriptStr]; // 获取网页的title NSString *title = [self.webView stringByEvaluatingJavaScriptFromString:@"document.title"]
与UIWebview同样,仅需三步:记住导入(#import <WebKit/WebKit.h>)github
- (void)simpleExampleTest { // 1.建立webview,并设置大小,"20"为状态栏高度 WKWebView *webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height - 20)]; // 2.建立请求 NSMutableURLRequest *request =[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.cnblogs.com/mddblog/"]]; // 3.加载网页 [webView loadRequest:request]; // 最后将webView添加到界面 [self.view addSubview:webView]; }
loadFileURL
函数,顾名思义加载本地文件。/// 模拟器调试加载mac本地文件 - (void)loadLocalFile { // 1.建立webview,并设置大小,"20"为状态栏高度 WKWebView *webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height - 20)]; // 2.建立url userName:电脑用户名 NSURL *url = [NSURL fileURLWithPath:@"/Users/userName/Desktop/bigIcon.png"]; // 3.加载文件 [webView loadFileURL:url allowingReadAccessToURL:url]; // 最后将webView添加到界面 [self.view addSubview:webView]; }
/// 其它三个加载函数 - (WKNavigation *)loadRequest:(NSURLRequest *)request; - (WKNavigation *)loadHTMLString:(NSString *)string baseURL:(nullable NSURL *)baseURL; - (WKNavigation *)loadData:(NSData *)data MIMEType:(NSString *)MIMEType characterEncodingName:(NSString *)characterEncodingName baseURL:(NSURL *)baseURL;
reloadFromOrigin
和goToBackForwardListItem
。
@property (nonatomic, readonly) BOOL canGoBack; @property (nonatomic, readonly) BOOL canGoForward; - (WKNavigation *)goBack; - (WKNavigation *)goForward; - (WKNavigation *)reload; - (WKNavigation *)reloadFromOrigin; // 增长的函数 - (WKNavigation *)goToBackForwardListItem:(WKBackForwardListItem *)item; // 增长的函数 - (void)stopLoading;
一共有三个代理协议:web
三个是否容许加载函数:ajax
/// 接收到服务器跳转请求以后调用 (服务器端redirect),不必定调用 - (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation; /// 3 在收到服务器的响应头,根据response相关信息,决定是否跳转。decisionHandler必须调用,来决定是否跳转,参数WKNavigationActionPolicyCancel取消跳转,WKNavigationActionPolicyAllow容许跳转 - (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler; /// 1 在发送请求以前,决定是否跳转 - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler;
追踪加载过程函数:api
/// 2 页面开始加载 - (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation; /// 4 开始获取到网页内容时返回 - (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation; /// 5 页面加载完成以后调用 - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation; /// 页面加载失败时调用 - (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation;
/// message: 收到的脚本信息. - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message;
/// 建立一个新的WebView - (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures; /// 输入框 - (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(nullable NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString * __nullable result))completionHandler; /// 确认框 - (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL result))completionHandler; /// 警告框 - (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler;
file://
则加载本地文件,http://
则加载网络内容,若是二者都不是则搜索输入的关键字。/// 控件高度 #define kSearchBarH 44 #define kBottomViewH 44 /// 屏幕大小尺寸 #define kScreenWidth [UIScreen mainScreen].bounds.size.width #define kScreenHeight [UIScreen mainScreen].bounds.size.height #import "ViewController.h" #import <WebKit/WebKit.h> @interface ViewController () <UISearchBarDelegate, WKNavigationDelegate> @property (nonatomic, strong) UISearchBar *searchBar; /// 网页控制导航栏 @property (weak, nonatomic) UIView *bottomView; @property (nonatomic, strong) WKWebView *wkWebView; @property (weak, nonatomic) UIButton *backBtn; @property (weak, nonatomic) UIButton *forwardBtn; @property (weak, nonatomic) UIButton *reloadBtn; @property (weak, nonatomic) UIButton *browserBtn; @property (weak, nonatomic) NSString *baseURLString; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // [self simpleExampleTest]; [self addSubViews]; [self refreshBottomButtonState]; [self.wkWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.cnblogs.com/mddblog/"]]]; } - (void)simpleExampleTest { // 1.建立webview,并设置大小,"20"为状态栏高度 WKWebView *webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height - 20)]; // 2.建立请求 NSMutableURLRequest *request =[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.cnblogs.com/mddblog/"]]; // // 3.加载网页 [webView loadRequest:request]; // [webView loadFileURL:[NSURL fileURLWithPath:@"/Users/userName/Desktop/bigIcon.png"] allowingReadAccessToURL:[NSURL fileURLWithPath:@"/Users/userName/Desktop/bigIcon.png"]]; // 最后将webView添加到界面 [self.view addSubview:webView]; } /// 模拟器加载mac本地文件 - (void)loadLocalFile { // 1.建立webview,并设置大小,"20"为状态栏高度 WKWebView *webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height - 20)]; // 2.建立url userName:电脑用户名 NSURL *url = [NSURL fileURLWithPath:@"/Users/userName/Desktop/bigIcon.png"]; // 3.加载文件 [webView loadFileURL:url allowingReadAccessToURL:url]; // 最后将webView添加到界面 [self.view addSubview:webView]; } - (void)addSubViews { [self addBottomViewButtons]; [self.view addSubview:self.searchBar]; [self.view addSubview:self.wkWebView]; } - (void)addBottomViewButtons { // 记录按钮个数 int count = 0; // 添加按钮 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; [button setTitle:@"后退" forState:UIControlStateNormal]; [button setTitleColor:[UIColor colorWithRed:249 / 255.0 green:102 / 255.0 blue:129 / 255.0 alpha:1.0] forState:UIControlStateNormal]; [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted]; [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateDisabled]; [button.titleLabel setFont:[UIFont systemFontOfSize:15]]; button.tag = ++count; // 标记按钮 [button addTarget:self action:@selector(onBottomButtonsClicled:) forControlEvents:UIControlEventTouchUpInside]; [self.bottomView addSubview:button]; self.backBtn = button; button = [UIButton buttonWithType:UIButtonTypeCustom]; [button setTitle:@"前进" forState:UIControlStateNormal]; [button setTitleColor:[UIColor colorWithRed:249 / 255.0 green:102 / 255.0 blue:129 / 255.0 alpha:1.0] forState:UIControlStateNormal]; [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted]; [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateDisabled]; [button.titleLabel setFont:[UIFont systemFontOfSize:15]]; button.tag = ++count; [button addTarget:self action:@selector(onBottomButtonsClicled:) forControlEvents:UIControlEventTouchUpInside]; [self.bottomView addSubview:button]; self.forwardBtn = button; button = [UIButton buttonWithType:UIButtonTypeCustom]; [button setTitle:@"从新加载" forState:UIControlStateNormal]; [button setTitleColor:[UIColor colorWithRed:249 / 255.0 green:102 / 255.0 blue:129 / 255.0 alpha:1.0] forState:UIControlStateNormal]; [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted]; [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateDisabled]; [button.titleLabel setFont:[UIFont systemFontOfSize:15]]; button.tag = ++count; [button addTarget:self action:@selector(onBottomButtonsClicled:) forControlEvents:UIControlEventTouchUpInside]; [self.bottomView addSubview:button]; self.reloadBtn = button; button = [UIButton buttonWithType:UIButtonTypeCustom]; [button setTitle:@"Safari" forState:UIControlStateNormal]; [button setTitleColor:[UIColor colorWithRed:249 / 255.0 green:102 / 255.0 blue:129 / 255.0 alpha:1.0] forState:UIControlStateNormal]; [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted]; [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateDisabled]; [button.titleLabel setFont:[UIFont systemFontOfSize:15]]; button.tag = ++count; [button addTarget:self action:@selector(onBottomButtonsClicled:) forControlEvents:UIControlEventTouchUpInside]; [self.bottomView addSubview:button]; self.browserBtn = button; // 统一设置frame [self setupBottomViewLayout]; } - (void)setupBottomViewLayout { int count = 4; CGFloat btnW = 80; CGFloat btnH = 30; CGFloat btnY = (self.bottomView.bounds.size.height - btnH) / 2; // 按钮间间隙 CGFloat margin = (self.bottomView.bounds.size.width - btnW * count) / count; CGFloat btnX = margin * 0.5; self.backBtn.frame = CGRectMake(btnX, btnY, btnW, btnH); btnX = self.backBtn.frame.origin.x + btnW + margin; self.forwardBtn.frame = CGRectMake(btnX, btnY, btnW, btnH); btnX = self.forwardBtn.frame.origin.x + btnW + margin; self.reloadBtn.frame = CGRectMake(btnX, btnY, btnW, btnH); btnX = self.reloadBtn.frame.origin.x + btnW + margin; self.browserBtn.frame = CGRectMake(btnX, btnY, btnW, btnH); } /// 刷新按钮是否容许点击 - (void)refreshBottomButtonState { if ([self.wkWebView canGoBack]) { self.backBtn.enabled = YES; } else { self.backBtn.enabled = NO; } if ([self.wkWebView canGoForward]) { self.forwardBtn.enabled = YES; } else { self.forwardBtn.enabled = NO; } } /// 按钮点击事件 - (void)onBottomButtonsClicled:(UIButton *)sender { switch (sender.tag) { case 1: { [self.wkWebView goBack]; [self refreshBottomButtonState]; } break; case 2: { [self.wkWebView goForward]; [self refreshBottomButtonState]; } break; case 3: [self.wkWebView reload]; break; case 4: [[UIApplication sharedApplication] openURL:self.wkWebView.URL]; break; default: break; } } #pragma mark - WKWebView WKNavigationDelegate 相关 /// 是否容许加载网页 在发送请求以前,决定是否跳转 - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler { NSString *urlString = [[navigationAction.request URL] absoluteString]; urlString = [urlString stringByRemovingPercentEncoding]; // NSLog(@"urlString=%@",urlString); // 用://截取字符串 NSArray *urlComps = [urlString componentsSeparatedByString:@"://"]; if ([urlComps count]) { // 获取协议头 NSString *protocolHead = [urlComps objectAtIndex:0]; NSLog(@"protocolHead=%@",protocolHead); } decisionHandler(WKNavigationActionPolicyAllow); } #pragma mark - searchBar代理方法 /// 点击搜索按钮 - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar { // 建立url NSURL *url = nil; NSString *urlStr = searchBar.text; // 若是file://则为打开bundle本地文件,http则为网站,不然只是通常搜索关键字 if([urlStr hasPrefix:@"file://"]){ NSRange range = [urlStr rangeOfString:@"file://"]; NSString *fileName = [urlStr substringFromIndex:range.length]; url = [[NSBundle mainBundle] URLForResource:fileName withExtension:nil]; // 若是是模拟器加载电脑上的文件,则用下面的代码 // url = [NSURL fileURLWithPath:fileName]; }else if(urlStr.length>0){ if ([urlStr hasPrefix:@"http://"]) { url=[NSURL URLWithString:urlStr]; } else { urlStr=[NSString stringWithFormat:@"http://www.baidu.com/s?wd=%@",urlStr]; } urlStr = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]; url=[NSURL URLWithString:urlStr]; } NSURLRequest *request=[NSURLRequest requestWithURL:url]; // 加载请求页面 [self.wkWebView loadRequest:request]; } #pragma mark - 懒加载 - (UIView *)bottomView { if (_bottomView == nil) { UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight - kBottomViewH, kScreenWidth, kBottomViewH)]; view.backgroundColor = [UIColor colorWithRed:230/255.0 green:230/255.0 blue:230/255.0 alpha:1]; [self.view addSubview:view]; _bottomView = view; } return _bottomView; } - (UISearchBar *)searchBar { if (_searchBar == nil) { UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 20, kScreenWidth, kSearchBarH)]; searchBar.delegate = self; searchBar.text = @"http://www.cnblogs.com/mddblog/"; _searchBar = searchBar; } return _searchBar; } - (WKWebView *)wkWebView { if (_wkWebView == nil) { WKWebView *webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 20 + kSearchBarH, kScreenWidth, kScreenHeight - 20 - kSearchBarH - kBottomViewH)]; webView.navigationDelegate = self; // webView.scrollView.scrollEnabled = NO; // webView.backgroundColor = [UIColor colorWithPatternImage:self.image]; // 容许左右划手势导航,默认容许 webView.allowsBackForwardNavigationGestures = YES; _wkWebView = webView; } return _wkWebView; } @end
iOS 8 推出 WKWebView 以及相关特性,告诉开发者提升多么多么大的性能,实测告诉你WKWebView的性能有多好!缓存
为了验证,我在项目中的分别 使用UIWebView 和 WKWebView 测试,来回加载十多个网页 产生的内存情况以下:
UIWebView
对比发现 WKWebview 这货 在persistent 常驻内存 使用得少多了,想一想看,一个app去哪儿省下几十M的内存,再加上用户量分布
iOS 9 60%
iOS 8 30%
iOS 7 6%左右
因此我建议能换WKWebview 就换吧,省下一大块内存减小没必要要的异常bug。
分析移动端H5性能数据,首先咱们说说是哪些性能数据。
什么是资源时序数据呢?每一个网页是有不少个资源组成的,有.js、.png、.css、.script等等,咱们就须要将这些每一个资源连接的耗时拿到,是什么类型的资源,完整连接;对于客户来讲有了这些还不够,还须要JS错误,页面的ajax请求。JS错误获取的固然是堆栈信息和错误类型。ajax请求通常是获取三个时间,响应时间,ajax下载时间,ajax回调时间。
上面分析的是可以获取到移动端H5的性能数据,这些数据怎么获得就是接下来要讲的了。数据获取是须要js来作的,都知道移动端是经过webView来加载网页的,js里面能经过performance这个接口来从webView内部api获取上面的那些数据,js获取的数据而后发给OC;那JS怎么样才能拿到这些数据呢,这就是最关键的,OC代码如何写才能让JS获取数据。
有两种方法能够获得数据,先介绍用NSURLProtocol这个类获取数据。
iOS的UIWebView加载网页的时候会走进NSURLProtocol这个类,因此咱们就须要在这个类里面做文章了,咱们先用UIWebView加载一个连接,例如百度等等,而后建立一个继承NSURLProtocol的类。
NSURLProtocol里面有不少方法,就不一一介绍了,有一个startLoading的方法,咱们在这个方法里面用NSURLConnection发送一个请求,设置代理,请求的request就是加载网页的request,由于NSURLProtocol有一个NSURLRequest的属性。
- (instancetype)initWithRequest:(NSURLRequest*)request delegate:(id)delegate startImmediately:(BOOL)startImmediately
这个就是请求的方法。
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
经过NSURLConnection设置代理,就须要写出代理方法,在其中一个代理方法里面能得到网页的代码,这就是咱们最关键的地方,就是上面这个方法,将data用utf-8转码就会获得代码。
获得网页的代码有什么用呢?熟悉网页端代码的都知道,网页端的代码都是由不少标签组成,咱们先找到<head>这个标签,在下面插入一个<script>标签,里面放入js代码,这个js代码就是用来获取上面介绍的性能数据。
- (void)URLProtocol:(NSURLProtocol *)protocol didLoadData:(NSData *)data;
上面会用到不少这个方法,为何要用这个呢,由于你注入了新的代码,你须要将这个新的网页代码用这个方法加载一下,否则网页会加载不出来的。
最后只须要将MonitorURLProtocol在appDelegate里面注册一下就能够了。
以上是经过NSURLProtocol这个类注入获取数据的代码,将JS代码注入后就须要JS将数据发送给咱们,能够经过交互这些方式,接下来我就介绍一种直白的交互,大多数的交互也是这么作的,只不过封装了,考虑的状况更多,我就只是根据实际状况来作了。
交互都会有一个标识,OC传一个标识给JS,JS经过标识判断,发送给你想要的数据。我首先是经过运行时动态加载的方法将加载连接的三个方法给hook,进入同一个我定义的方法,而后在这个方法里面传标识给JS。
将标识和block做为键值对存起来,而后将JS将数据用url的形式发过来,咱们取出来,匹配一下对应相应的标识,而后用block回调相应的数据,相应的代码这里就不贴了,最后我会给github上代码的连接。接受url就是用UIWebView的代理方法。
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
{
[WebViewTracker webView:webView withUrl:request.URL];
return YES;
}
咱们还能够在didFinishLoad里面手动调用JS代码来获取获取数据,和NSURLProtocol结合起来用,各有各的优缺点,一个是在顶部注入代码,一个是在尾部调用代码,都是会丢失一部分数据,因此最好的办法就是结合起来用。
以上是本身获取这些数据,若是是作成SDK监控APP获取这些数据,就不同了,须要去hookUIWebView的代理方法,hook静态库的方法和普通的运行时加载是不同的,这个我能够在之后的文章里面介绍。
WKWebView是iOS8.0之后出来的,目前使用的人还不是不少,可是这个比UIWebView性能方面好不少,从内核到内存优化等等,可是因为还有iOS7如下的用户,因此面使用的人很少,可是这个是趋势。WKWebView获取上面的性能数据是如出一辙的,不一样的是WKWebView不会走进NSURLProtocol,因此实在didFinishLoad里面手动调用,这里就不详细说了,直接给代码连接
https://github.com/LonelyWise/WebViewMonitor
WKWebView
WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init]; config.allowsInlineMediaPlayback = YES; config.mediaPlaybackRequiresUserAction = false; wkWebView=[[WKWebView alloc] initWithFrame:rect configuration:config]; wkWebView.UIDelegate=self; wkWebView.navigationDelegate=self;
UIWebVIew
[self.webView setMediaPlaybackRequiresUserAction:NO];