最近沒事看了一下 WKWebView ,就把本身總結的記錄一下,方便查看.web
注:一下爲我的總結,會有不足,熱烈歡迎大神指點服務器
1> 建立
導入:#import <WebKit/WebKit.h>
WKWebView *webView = [[WKWebView alloc] initWithFrame:self.view.bounds];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.baidu.com"]]];
[self.view addSubview:webView];ide
2> WKWebView的代理有三個
WKUIDelegate,WKNavigationDelegate,WKScriptMessageHandler代理
3> 代理方法
3.1> WKUIDelegate的代理方法
/** 建立新的webView 指定配置對象、導航動做對象、window特性 */
-(WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures{}
/** 顯示一個與JS交互的確認Alert(彈框) */
- (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL result))completionHandler{}
/** 彈出一個輸入框(與JS交互的) */
- (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(nullable NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString * _Nullable result))completionHandler{}
/** 顯示一個與JS交互的警告Alert(彈框) */
- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler{}
/** webview關閉時的回調 */
- (void)webViewDidClose:(WKWebView *)webView{}
3.2> WKNavigationDelegate代理方法
#pragma mark 導航監聽
/** 在發送請求以前,決定是否跳轉 */
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{}
/** 收到身份驗證 */
- (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential))completionHandler{}
/** 在收到響應後,決定是否跳轉 */
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{}
/** 接收到服務器跳轉請求以後再執行 */
- (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(null_unspecified WKNavigation *)navigation{}
/** 頁面加載失敗 */
- (void)webView:(WKWebView *)webView didFailNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error{}
/** WKWebView終止 */
- (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView{}對象
#pragma mark 網頁監聽
/** 頁面開始加載 */
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(null_unspecified WKNavigation *)navigation{}
/** 當內容開始返回時調用 */
- (void)webView:(WKWebView *)webView didCommitNavigation:(null_unspecified WKNavigation *)navigation{}
/** 頁面加載完成以後調用 */
- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation{}
/** 頁面加載失敗時調用 */
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error{}ip
3.3> WKScriptMessageHandler代理方法
/** 網頁經過JS把消息發送給OC */
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message{}ci