1. 程式人生 > >IOS-WKWebView的使用以及Cookie的處理

IOS-WKWebView的使用以及Cookie的處理

其實和UIWebView的用法沒什麼區別,但是WKWebView相對於UIWebView強大了很多,記憶體的消耗相對少了,所提供的介面也豐富了。

WKWebView的代理方法:

WKNavigationDelegate代理的使用:

該代理提供的方法,可以用來追蹤載入過程(頁面開始載入、載入完成、載入失敗)、決定是否執行跳轉。
// 頁面開始載入時呼叫
- (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;

頁面跳轉的代理方法有三種,分為(收到跳轉與決定是否跳轉兩種)

// 接收到伺服器跳轉請求之後呼叫
- (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;

代理方法的展開:

#pragma mark - WKNavigationDelegate -
/**
 *  頁面開始載入時呼叫
 *
 *  @param webView    實現該代理的webview
 *  @param navigation 當前navigation
 */
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
    NSLog(@"%s", __FUNCTION__);
}

/**
 *  當內容開始返回時呼叫
 *
 *  @param webView    實現該代理的webview
 *  @param navigation 當前navigation
 */
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation {
    NSLog(@"%s", __FUNCTION__);
}

/**
 *  頁面載入完成之後呼叫
 *
 *  @param webView    實現該代理的webview
 *  @param navigation 當前navigation
 */
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    self.progressView.alpha = 0;
    NSLog(@"%s", __FUNCTION__);
}

/**
 *  載入失敗時呼叫
 *
 *  @param webView    實現該代理的webview
 *  @param navigation 當前navigation
 *  @param error      錯誤
 */
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error 
{
    NSLog(@"%s", __FUNCTION__);
}

/**
 *  接收到伺服器跳轉請求之後呼叫
 *
 *  @param webView      實現該代理的webview
 *  @param navigation   當前navigation
 */
- (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation {

    NSLog(@"%s", __FUNCTION__);
}

/**
 *  在收到響應後,決定是否跳轉
 *
 *  @param webView            實現該代理的webview
 *  @param navigationResponse 當前navigation
 *  @param decisionHandler    是否跳轉block
 */
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler {

    NSString *clickUrl = [navigationResponse.response.URL absoluteString];
    self.barView.addressLabel.text = clickUrl;
    NSLog(@"%@", navigationResponse.response.URL);
    decisionHandler(WKNavigationResponsePolicyAllow);
}

/**
 *  在傳送請求之前,決定是否跳轉
 *
 *  @param webView          實現該代理的webview
 *  @param navigationAction 當前navigation
 *  @param decisionHandler  是否調轉block
 */
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {

    decisionHandler(WKNavigationActionPolicyAllow);
}

WKUIDelegate代理的使用:

// 建立一個新的WebView
- (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures;

剩下三個代理方法全都是與介面彈出提示框相關的,針對於web介面的三種提示框(警告框、確認框、輸入框)分別對應三種代理方法。下面只舉了警告框的例子。

#pragma mark - WKUIDelegate

/**
 *  web介面中有彈出警告框時呼叫
 *
 *  @param webView           實現該代理的webview
 *  @param message           警告框中的內容
 *  @param frame             主視窗
 *  @param completionHandler 警告框消失呼叫
 */
- (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL))completionHandler {

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"溫馨提示" message:message preferredStyle:(UIAlertControllerStyleAlert)];
    UIAlertAction *trueAction = [UIAlertAction actionWithTitle:@"確定" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction *action) {

    }];
    [alertController addAction:trueAction];
    [self presentViewController:alertController animated:YES completion:nil];

    completionHandler(YES);
}

- (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString *))completionHandler {

} 

WKScriptMessageHandler代理的使用:

這個協議中包含一個必須實現的方法,這個方法是提高App與web端互動的關鍵,它可以直接將接收到的JS指令碼轉為OC或Swift物件。(當然,在UIWebView也可以通過“曲線救國”的方式與web進行互動,著名的Cordova框架就是這種機制)。

#pragma mark - WKScriptMessageHandler
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {

    NSLog(@"%@", message);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

WKWebView載入JS:

// 圖片縮放的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:@""baseURL:nil];
[self.view addSubview:_webView];

WKWebView的Cookie問題: