1. 程式人生 > >IOS WKWebView初級使用 以及WKWebView進度條的新增

IOS WKWebView初級使用 以及WKWebView進度條的新增

WKWebView 相比UIWebView 在穩定性,效能,功能方面有很大提升 (戰友記憶體比webView小,缺點 不支援快取和NSURLProtocol)增加了載入進度條
標頭檔案 WebKit/WebKit.h這裡寫程式碼片

 // 建立一個webiview的配置項
    WKWebViewConfiguration *configuretion = [[WKWebViewConfiguration alloc] init];
    // 設定偏好設定
    configuretion.preferences = [[WKPreferences alloc]init];
    configuretion.preferences
.minimumFontSize = 10; configuretion.preferences.javaScriptEnabled = true; configuretion.processPool = [[WKProcessPool alloc]init]; // 通過js與webview內容互動配置 configuretion.userContentController = [[WKUserContentController alloc] init]; [UIApplication sharedApplication].networkActivityIndicatorVisible
= YES; 用來註冊JS裡面 的方法名 來接收js裡面觸發的回撥事件 //OC註冊供JS呼叫的方法 [configuretion.userContentController addScriptMessageHandler:self name:@"linkToItem"]; [configuretion.userContentController addScriptMessageHandler:self name:@"callHeight"]; // 預設是不能通過JS自動開啟視窗的,必須通過使用者互動才能開啟 configuretion.preferences.javaScriptCanOpenWindowsAutomatically
= NO; _webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(Bounds), CGRectGetHeight(Bounds)) configuration:configuretion];//self.view.bounds if (self.webUrl.length > 0) { [_webView loadRequest:[[NSURLRequest alloc] initWithURL:[NSURL URLWithString:self.webUrl]]]; } else { [_webView loadRequest:[[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://lushanghulian.com/Api/event"]]]; } [_webView goBack]; [_webView goForward]; _webView.navigationDelegate = self; _webView.UIDelegate = self; [self.view addSubview:_webView];

WKWebView 的代
//開始載入

- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
    [self showHUDWithHint:@""];
}

載入完成

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    [self hideHud];
    //獲得高度
    //    [webView evaluateJavaScript:@"document.body.offsetHeight;" completionHandler:^(id Result, NSError * error) {
    //
    //    }];
}

載入失敗

- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation
      withError:(NSError *)error {
    [self hideHud];
}

回撥事件 message.body 傳遞引數

#pragma mark - WKScriptMessageHandler
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
    NSLog(@"方法名:%@", message.name);
    NSLog(@"引數:%@", message.body);
}

如何給webView的載入進度
通過監聽webView estimatedProgress 屬性

[_webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];

移除監聽

- (void)dealloc {
   [self.webView removeObserver:self forKeyPath:@"estimatedProgress"];
}

//監聽的代理

//UIProgressView 這個自己寫一個進度條就可以根據監聽改變就行
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if (object == self.webView && [keyPath isEqualToString:@"estimatedProgress"]) {
        CGFloat newprogress = [[change objectForKey:NSKeyValueChangeNewKey] doubleValue];
        if (newprogress == 1) {
            self.progressView.hidden = YES;
            [self.progressView setProgress:0 animated:NO];
        }else {
            self.progressView.hidden = NO;
            [self.progressView setProgress:newprogress animated:YES];
        }
    }
}