之前把基本的學會了,這次該研究研究其他的功能,想想別的app上都有哪些功能,試著實現以下:
1.看文檔找找 這個屬性的意思是估算加載的進度類型是double,只讀的看來可以用它來實現網頁加載的進度展示
estimatedProgress //An estimate of what fraction of the current navigation has been loaded.
想一想,加載網頁一般上面有一條藍線來顯示進度,就實現這個吧
//先弄個屬性
@property (strong, nonatomic) UIProgressView *progressView;
//添加到視圖上
[self.view addSubview:self.progressView];
懶加載一下吧,讓它在狀態欄下面顯示,樣式就默認吧
-(UIProgressView *)progressView{
if (_progressView==nil) {
_progressView= [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleDefault];
_progressView.frame=CGRectMake(0, 20, self.view.bounds.size.width, 2);
}
return _progressView;
}
接下來就是監聽這個屬性值得變化,然后賦值給UIProgressView的屬性上讓他展示出來進度,思路就是這樣,我那個偷懶就用ReactiveCocoa(這個框架真的很厲害) 框架了,上代碼
//這里是就實現了監聽綁定信號
RACSignal *sign=RACObserve(self.webView, estimatedProgress);
//這里就可以拿來用每次值改變都會調這個block里的代碼
@weakify(self);
[sign subscribeNext:^(id x) {
@strongify(self);
//當進度為一完全加載完就讓進度條隱藏吧
if ([x floatValue]==1) {
self.progressView.hidden=YES;
}
//復值
self.progressView.progress=[x floatValue];
}];
記得在每次新請求的時候把progressView顯示出來,加上一句
-(void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
NSLog(@"%s",__FUNCTION__);
self.progressView.hidden=NO;
self.hud=[MBProgressHUD showHUDAddedTo:self.view animated:YES];
}
算上上一篇的基本效果 進度條實現了 來看一下效果
2.點著點著頁面發現問題了,凡是另開一個網頁的點擊都不好用了,在當前頁面刷新的可以用,解決吧。。。
先開始找屬性,看有沒有允許跳轉頁面重新reload的屬性,找了半天沒發現,再找代理吧,感覺這是導航出現的問題,找WKNavigationDelegate的代理方法試試。
找到這倆個能決定是否允許一個導航
- webView:decidePolicyForNavigationAction:decisionHandler:
//Decides whether to allow or cancel a navigation. - webView:decidePolicyForNavigationResponse:decisionHandler:
//Decides whether to allow or cancel a navigation after its response is known.
看看代理給里面參數里面是否有能判斷出是否是打開一個新的頁面
WKNavigationAction這個參數類里面有這么一個屬性 嘿嘿,說是導航若開一個新窗口值為nil,先來試試
//The target frame, or nil if this is a new window navigation.
@property (nullable, nonatomic, readonly, copy) WKFrameInfo *targetFrame;
代碼打印一下到底有啥區別
-(void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
NSLog(@"targetFrame%@",navigationAction.targetFrame);
}
一運行崩了。。。。仔細一看應該是后面block參數沒有用,看了下解釋明白里面既然這是決定是否允許訪問的代理,當然要回調一下啊。。。加上下面一句,參數是個枚舉類型,一看就懂了,一個是允許,一個是取消。那這個地方可以做一些訪問的過濾 來個允許:
// WKNavigationActionPolicyCancel,
// WKNavigationActionPolicyAllow,
-(void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
NSLog(@"targetFrame%@",navigationAction.targetFrame);
decisionHandler(WKNavigationActionPolicyAllow);
}
然后點擊看看有啥區別
1.點擊本頁跳轉打印的東西
2.點擊那個不跳的打印的東西 哈哈
這樣能判斷出來了吧,再就是解決讓他跳啊,最終代理里代碼如下
-(void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
NSLog(@"targetFrame%@",navigationAction.targetFrame);
if (navigationAction.targetFrame==nil) {
[self.webView loadRequest:navigationAction.request];
}
decisionHandler(WKNavigationActionPolicyAllow);
}
點進去試試,成功~效果:
3.點進去后又有需求了....我要回去之前的頁面啊,接著踩坑吧
碰巧最近發現一個比較好用的開源---讓任意View都可以拖動 - WMDragView(就一個類,很簡單實用)
正好練習一下,用這個做一個返回按鈕
引入頭文件 設置一個屬性
#import "WMDragView.h"
@property (strong, nonatomic) WMDragView *backView;
添加進View中
//給他個位置
self.backView = [[WMDragView alloc] initWithFrame:CGRectMake(self.view.frame.size.width-50, self.view.frame.size.height-150, 50, 50)];
//圓角
self.backView.layer.cornerRadius = 25;
self.backView.backgroundColor= [UIColor colorWithRed:(223.0)/255.0 green:(107.0)/255.0 blue:(93.0)/255.0 alpha:0.8f];
//是不是保持在邊界,默認為NO,沒有黏貼邊界效果
self.backView.isKeepBounds = YES;
//設置本地圖片,可以設置網絡圖片
self.backView.imageView.image=[UIImage imageNamed:@"back"];
///點擊事件
@weakify(self);
self.backView.ClickDragViewBlock = ^(WMDragView *dragView){
@strongify(self);
[self.webView goBack];
};
//限定logoView的活動范圍
self.backView.freeRect = CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height-64);
[self.view addSubview:self.backView];
效果圖片
接下來該解決返回的問題 有這個方法goBack,還有goForward直接用,很簡單...
[self.webView goBack];
這下基本該有的應該都有了