WKWebView.14年就出來了.用的少.并不是很熟練.聽說性能比UIWebView好很多.將內核啥的集成了..就是牛X了
直接談使用吧.
@property (nonatomic, strong) WKWebView *webView; @property (nonatomic, strong) UIProgressView *progressView;
self.webView = [[WKWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - 46)]; self.webView.navigationDelegate = self; self.webView.UIDelegate = self; [self.webView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight]; [self.webView setMultipleTouchEnabled:YES]; [self.webView setAutoresizesSubviews:YES]; [self.webView.scrollView setAlwaysBounceVertical:YES]; self.webView.scrollView.delegate = self;//監(jiān)聽偏移值. [self.webView.scrollView addSubview:self.indicatorView];//自己寫的 [self.webView.scrollView setContentOffset:CGPointMake(0, 100)]; [self.webView setAllowsBackForwardNavigationGestures:true];//側滑返回webView的上一級
就是這么實在. 可以直接拷貝使用
幾個代理方法.
頁面開始加載的時候調用
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{}
當內容開始返回時調用
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation { //這里處理返回按鈕.默.然后根據webView加載情況判斷是否顯示或隱藏. self.backButton.hidden = !webView.canGoBack; }
// 頁面加載完成之后調用-->比較多的處理在這里操作.
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation { [self reloadView]; [self.webView.scrollView setContentOffset:CGPointMake(0, 0)]; if ( self.isLoad == YES) { [self hideHUD]; } }
// 頁面加載失敗時調用
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation { [self hideHUD]; }
添加觀察者.監(jiān)聽title.加載進度.還有一個下拉偏移值(這個是做刷新準備的.也可以使用self.webView.scrollView的偏移代理做.本來這個不需要的.如果有網的情況.是會自動加載出來的.項目經理說有時候刷不出來了,那就按照他的意思,加一個刷新效果.下面介紹思路)
[self.webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];`
[self.webView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:NULL];`
[self.webView.scrollView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];`
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if ([keyPath isEqualToString:@"estimatedProgress"]) {//監(jiān)聽進度 if (object == self.webView) { [self.progressView setAlpha:1.0f]; [self.progressView setProgress:self.webView.estimatedProgress animated:YES]; if(self.webView.estimatedProgress >= 1.0f) { [UIView animateWithDuration:0.3 delay:0.3 options:UIViewAnimationOptionCurveEaseOut animations:^{ [self.progressView setAlpha:0.0f]; } completion:^(BOOL finished) { [self.progressView setProgress:0.0f animated:NO]; }]; } } else { [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; } } else if ([keyPath isEqualToString:@"title"])//監(jiān)聽標題 { if (object == self.webView) { if ([self.webView.title fq_IsEqualToString:@"綜合"]|| [self.webView.title fq_IsEqualToString:@"活動"]|| [self.webView.title fq_IsEqualToString:@"產品"]|| [self.webView.title fq_IsEqualToString:@"博客"]) { self.title = GETLANGUAGES(self.webView.title, nil); }else{ self.title = self.webView.title; } } else { [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; } } else if(object == self.webView.scrollView && [keyPath isEqualToString:@"contentOffset"]){//監(jiān)聽偏移值.我是為了做刷新處理. CGPoint offset = [change[@"new"] CGPointValue]; self.indicatorView.progress = fabs(offset.y) / 100.0; if (!fabs(offset.y)) { [self.indicatorView stopAnimation]; } if (-offset.y >= 100) { self.webBackView.frame = CGRectMake(0,0, self.view.bounds.size.width, 100); self.webBackView.alpha = 1.0f; } }else{ [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; } }
目前沒有什么js與oc交互.因為全部是url跳轉的.所以我的做法是攔截url.然后做有網沒網提示.或者對應的跳轉處理.
-(void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{}
這里說一下給webView刷新界面的思路:
1.首先創(chuàng)建一個View.添加到self.view上面.
2.然后當webView下拉到一定層度的時候.我們根據contentOffset的y值來判斷是否需要刷新了.前面有監(jiān)聽到偏移值contentOffset
3.比較難實現(xiàn)的就是.下拉讓其停頓2s以后,再讓webView自動還原到原來的位置,如果還是在觀察者里面去更改webView的contentOffset這樣就會循環(huán)引用
4.我的解決方法是在scrollView的代理里面做一個判斷和操作.讓整個webview向下偏移.然后讓我們添加在self.view上面的視圖顯示.并且在上面添加一個動畫效果.菊花什么.也可以是文字"正在加載系列",刷新界面就很簡單了.重新加載url即可.
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{ //如果scrollView的一個值達到多少. self.indicatorView.alpha = 1.0f; if((scrollView.contentOffset.y > 0) && self.webView.frame.origin.y != 0){ self.webBackView.alpha= 0.0f; self.webView.frame = CGRectMake(0, 100-scrollView.contentOffset.y, screen_width,self.view.bounds.size.height - 46); [self.webView.scrollView setContentOffset:CGPointMake(0,scrollView.contentOffset.y)]; } }
這個是為了防止部分網頁可以縮放的處理
-(void)scrollViewDidZoom:(UIScrollView *)scrollView { self.webBackView.alpha = 0.0f; }
主要是這個方法
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{ if (-scrollView.contentOffset.y >= 100) { self.indicatorView.alpha = 0.0f; [UIView animateWithDuration:0.5 animations:^{ self.webView.frame = CGRectMake(0, 100, screen_width,self.view.bounds.size.height - 46); }completion:^(BOOL finished) { self.webBackView.frame = CGRectMake(0, 0, screen_width, 100); [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.urlStr]]]; }]; //兩秒鐘以后.執(zhí)行另一個方法.讓他還遠 [self performSelector:@selector(reloadView) withObject:nil afterDelay:2.0]; }else{ } }
其實就是兩層view.一個添加在webView的scrollView上面.讓其layer偏移到其上面.跟隨一起移動即可,
另外一個View添加在self.view上面
如果有什么更好的方式給webView刷新界面.可以共同學習.