開發App的過程當中,經常會遇到在App內部加載網頁,一般用
UIWebView
加載。這個自iOS2開始使用的網頁加載器一直是開發的心病
:加載速度慢,佔用內存多,優化困難。若是加載網頁多,還可能由於過量佔用內存而給系統kill
掉。各類優化的方法效果也不那麼明顯(點擊查看經常使用優化方法)。html
iOS8之後,蘋果推出了新框架Wekkit
,提供了替換UIWebView
的組件WKWebView
。各類UIWebView
的問題沒有了,速度更快了,佔用內存少了,一句話,WKWebView
是App內部加載網頁的最佳選擇!java
先看下 WKWebView
的特性:git
- 在性能、穩定性、功能方面有很大提高(最直觀的體現就是加載網頁是佔用的內存,模擬器加載百度與開源中國網站時,WKWebView佔用23M,而UIWebView佔用85M);
- 容許JavaScript的Nitro庫加載並使用(UIWebView中限制);
- 支持了更多的HTML5特性;
- 高達60fps的滾動刷新率以及內置手勢;
- 將UIWebViewDelegate與UIWebView重構成了14類與3個協議(查看蘋果官方文檔);
而後從如下幾個方面說下WKWebView
的基本用法:github
- 加載網頁
- 加載的狀態回調
- 新的
WKUIDelegate
協議 - 動態加載並運行
JS
代碼 - webView 執行
JS
代碼 JS
調用App註冊過的方法
1、加載網頁
加載網頁或HTML代碼的方式與UIWebView
相同,代碼示例以下:web
1
2
3
|
WKWebView *webView =
[[WKWebView alloc] initWithFrame:self.view.bounds];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com"]]];
[self.view addSubview:webView];
|
2、加載的狀態回調 (WKNavigationDelegate)
用來追蹤加載過程(頁面開始加載、加載完成、加載失敗)的方法:數組
1
2
3
4
5
6
7
8
|
// 頁面開始加載時調用
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation;
// 當內容開始返回時調用
-
(void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation;
// 頁面加載完成以後調用
-
(void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation;
// 頁面加載失敗時調用
-
(void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation;
|
頁面跳轉的代理方法:服務器
1
2
3
4
5
6
|
// 接收到服務器跳轉請求以後調用
- (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation;
// 在收到響應後,決定是否跳轉
-
(void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler;
// 在發送請求以前,決定是否跳轉
-
(void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler;
|
3、新的WKUIDelegate
協議
這個協議主要用於WKWebView
處理web界面的三種提示框(警告框、確認框、輸入框),下面是警告框的例子:app
1
2
3
4
5
6
7
8
9
|
/**
* web界面中有彈出警告框時調用
*
*
@param webView 實現該代理的webview
*
@param message 警告框中的內容
*
@param frame 主窗口
*
@param completionHandler 警告框消失調用
*/
- (
void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(void (^)())completionHandler;
|
4、動態加載並運行JS
代碼
用於在客戶端內部加入JS
代碼,並執行,示例以下:框架
1
2
3
4
5
6
7
8
9
10
|
// 圖片縮放的js代碼
NSString *js = @"var count = document.images.length;for (var i = 0; i < count; i++) {var image = document.images[i];image.style.width=320;};window.alert('找到' + count + '張圖');";
// 根據JS字符串初始化WKUserScript對象
WKUserScript *script = [[WKUserScript alloc] initWithSource:js injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:
YES];
// 根據生成的WKUserScript對象,初始化WKWebViewConfiguration
WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
[config
.userContentController addUserScript:script];
_webView = [[WKWebView alloc] initWithFrame:
self.view.bounds configuration:config];
[_webView loadHTMLString:
@"<head></head><imgea src='http://www.nsu.edu.cn/v/2014v3/img/background/3.jpg' />"baseURL:nil];
[
self.view addSubview:_webView];
|
5、webView 執行JS
代碼
用戶調用用JS
寫過的代碼,通常指服務端開發的:異步
1
2
|
//javaScriptString是JS方法名,completionHandler是異步回調block
[
self.webView evaluateJavaScript:javaScriptString completionHandler:completionHandler];
|
6、JS
調用App註冊過的方法
再WKWebView
裏面註冊供JS調用的方法,是經過WKUserContentController
類下面的方法:
1
|
- (void)addScriptMessageHandler:(id <WKScriptMessageHandler>)scriptMessageHandler name:(NSString *)name;
|
scriptMessageHandler
是代理回調,JS
調用name
方法後,OC
會調用scriptMessageHandler
指定的對象。
JS
在調用OC
註冊方法的時候要用下面的方式:
1
|
window.webkit.messageHandlers.
<name>.postMessage(<messageBody>)
|
注意,name(方法名)是放在中間的,messageBody只能是一個對象,若是要傳多個值,須要封裝成數組,或者字典。整個示例以下:
1
2
3
4
5
6
7
8
9
10
11
|
//OC註冊供JS調用的方法
[[_webView configuration].userContentController addScriptMessageHandler:self name:@"closeMe"];
//OC在JS調用方法作的處理
- (void)userContentController
:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message
{
NSLog(@"JS 調用了 %@ 方法,傳回參數 %@",message.name,message.body);
}
//JS調用
window
.webkit.messageHandlers.closeMe.postMessage(null);
|
若是你在self
的dealloc
打個斷點,會發現self
沒有釋放!這顯然是不行的!谷歌後看到一種解決方法,以下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
@interface WeakScriptMessageDelegate : NSObject<WKScriptMessageHandler>
@property (nonatomic, weak) id<WKScriptMessageHandler> scriptDelegate;
- (instancetype)initWithDelegate:(
id<WKScriptMessageHandler>)scriptDelegate;
@end
@implementation WeakScriptMessageDelegate
- (instancetype)initWithDelegate:(
id<WKScriptMessageHandler>)scriptDelegate
{
self = [super init];
if (self) {
_scriptDelegate = scriptDelegate;
}
return self;
}
- (
void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message
{
[
self.scriptDelegate userContentController:userContentController didReceiveScriptMessage:message];
}
@end
|
思路是另外建立一個代理對象,而後經過代理對象回調指定的self
,
1
2
|
WKUserContentController *userContentController = [[WKUserContentController alloc] init];
[userContentController addScriptMessageHandler:[[
WeakScriptMessageDelegate alloc] initWithDelegate:self] name:@"closeMe"];
|
運行代碼,self
釋放了,WeakScriptMessageDelegate
卻沒有釋放啊啊啊!
還需在self
的dealloc
裏面 添加這樣一句代碼:
[[_webView configuration].userContentController removeScriptMessageHandlerForName:@」closeMe」];
OK,圓滿解決問題!
目前,大多數App須要支持iOS7以上的版本,而WKWebView
只在iOS8後才能用,因此須要一個兼容性方案,既iOS7下用UIWebView
,iOS8後用WKWebView
。這個庫提供了這種兼容性方案:https://github.com/wangyangcc/IMYWebView