WKWebView簡單使用,wkwebview
最近沒事看了一下 WKWebView ,就把自己總結的記錄一下,方便查看.
注:一下為個人總結,會有不足,熱烈歡迎大神指點
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];
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{}
3.3> WKScriptMessageHandler代理方法
/** 網頁通過JS把訊息發送給OC */
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message{}