一個KVO 實現WKWebView載入進度條的例子 (注意最後移除觀察者)

來源:互聯網
上載者:User

標籤:dig   完成   ini   nslog   mat   stomp   idfa   context   little   

////  OpenWebViewController.m//  Treasure////  Created by 藍藍色信子 on 16/7/29.//  Copyright ? 2016年 GY. All rights reserved.//#import "ZTOpenWebViewController.h"#import <WebKit/WebKit.h>@interface ZTOpenWebViewController ()<WKNavigationDelegate>//{//    //網頁視圖//    UIWebView * _webView;//}@property (strong, nonatomic) WKWebView *webView;@property (strong, nonatomic) UIProgressView *progressView;@end@implementation ZTOpenWebViewController- (void)viewDidLoad {    [super viewDidLoad];    //    //取消導覽列的影響    self.automaticallyAdjustsScrollViewInsets = NO;    self.view.backgroundColor = [UIColor whiteColor];        //[SVProgressHUD show];    //執行個體化    [self createWebView];        [self creatCustomProgressView];}-(void)creatCustomProgressView{        //增加載入進度條    // KVO,監聽webView屬性值得變化(estimatedProgress,title為特定的key)    [_webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];    [_webView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:nil];        // UIProgressView初始化    self.progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];    self.progressView.frame = CGRectMake(0, 0, _webView.frame.size.width, 5);    self.progressView.trackTintColor = [UIColor clearColor]; // 設定進度條的色彩    self.progressView.progressTintColor = [UIColor magentaColor];    // 設定初始的進度,防止使用者進來就懵逼了(大概也是一開始設定的10%的預設值)    [self.progressView setProgress:0.1 animated:YES];    [_webView addSubview:self.progressView];}- (void)viewWillAppear:(BOOL)animated{    [self.navigationController setNavigationBarHidden:NO animated:NO];    [super viewWillAppear:YES];}- (void)viewWillDisappear:(BOOL)animated{    [SVProgressHUD dismiss];    [super viewWillDisappear:animated];}- (void)createWebView{    _webView = [[WKWebView alloc]initWithFrame:self.view.bounds];        [self.view addSubview:_webView];        //調整適應比例    //_webView.scalesPageToFit = YES;    //設定代理    _webView.navigationDelegate = self;    [[NSURLCache sharedURLCache] removeAllCachedResponses];    [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.urlStr] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10.0]];    }#pragma mark - WKWebView NavigationDelegate//WKNavigationDelegate- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {    //NSLog(@"是否允許這個導航");    decisionHandler(WKNavigationActionPolicyAllow);}- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler {    //    Decides whether to allow or cancel a navigation after its response is known.        //NSLog(@"知道返回內容之後,是否允許載入,允許載入");    decisionHandler(WKNavigationResponsePolicyAllow);}- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(null_unspecified WKNavigation *)navigation {    //NSLog(@"開始載入");    //self.progress.alpha  = 1;        //[SVProgressHUD show];    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;    }- (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(null_unspecified WKNavigation *)navigation {    //NSLog(@"跳轉到其他的伺服器");    }- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error {    //NSLog(@"網頁由於某些原因載入失敗");    //[SVProgressHUD dismiss];    //self.progress.alpha  = 0;    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;}- (void)webView:(WKWebView *)webView didCommitNavigation:(null_unspecified WKNavigation *)navigation {    //NSLog(@"網頁開始接收網頁內容");}- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation {    //NSLog(@"網頁導航載入完畢");    //[SVProgressHUD dismiss];    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;        self.title = webView.title;    [webView evaluateJavaScript:@"document.title" completionHandler:^(id _Nullable ss, NSError * _Nullable error) {        //NSLog(@"----document.title:%@---webView title:%@",ss,webView.title);    }];    //self.progress.alpha  = 0;    }- (void)webView:(WKWebView *)webView didFailNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error {    //NSLog(@"載入失敗,失敗原因:%@",[error description]);    //self.progress.alpha = 0;}- (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView {    //NSLog(@"網頁載入內容進程終止");}#pragma mark - KVO監聽// 第三部:完成監聽方法- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {        if ([object isEqual:_webView] && [keyPath isEqualToString:@"estimatedProgress"]) { // 進度條                CGFloat newprogress = [[change objectForKey:NSKeyValueChangeNewKey] doubleValue];        //NSLog(@"列印測試進度值:%f", newprogress);                if (newprogress == 1) { // 載入完成            // 首先載入到頭            [self.progressView setProgress:newprogress animated:YES];            // 之後0.3秒延遲隱藏            __weak typeof(self) weakSelf = self;            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.3 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{                                weakSelf.progressView.hidden = YES;                [weakSelf.progressView setProgress:0 animated:NO];            });                    } else { // 載入中            self.progressView.hidden = NO;            [self.progressView setProgress:newprogress animated:YES];        }    } else if ([object isEqual:_webView] && [keyPath isEqualToString:@"title"]) { // 標題                //self.title = _webView.title;    } else { // 其他                [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];    }}-(void)dealloc{    NSLog(@"webVC dealloc = %@",self);    [_webView removeObserver:self forKeyPath:@"estimatedProgress"];    [_webView removeObserver:self forKeyPath:@"title"];}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}/* #pragma mark - Navigation  // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */@end

 

一個KVO 實現WKWebView載入進度條的例子 (注意最後移除觀察者)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.