WKWebView gets page title and load progress value

Keywords: iOS less

1. Foreword

After IOS 8, Apple launched a new framework Webkit, which provides a component to replace UIWebView, WKWebView. Compared with UIWebView, it has many advantages, faster speed and less memory.

2. Basic use

The usage method is similar to that of UIWebView. For details, please refer to Replace UIWebView with WKWebView , this article is very detailed, this is not the focus of this article, we focus on how to page title and load progress value.

3. Get page title and load progress values

//Import frame
#import <WebKit/WebKit.h>
/*********/
@property (nonatomic, strong) WKWebView *mWebView;//webView
@property (nonatomic, strong) UIProgressView *mProgressView;//progress bar
/*********/
- (void)viewDidLoad {
  [super viewDidLoad];
  [self initWebView];
}
  //Increase kvo monitoring to obtain page title and loading progress value
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.mWebView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:NULL];
    [self.mWebView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:NULL];
}
//KVO must be removed or it will crash
- (void)dealloc{
    [self.mWebView removeObserver:self forKeyPath:@"estimatedProgress"];
    [self.mWebView removeObserver:self forKeyPath:@"title"];
}

- (void)initWebView
{
    _mWebView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - TopFullHeight)];
    [_mWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http:xxxxx"]]];
    [self.view addSubview:_mWebView];

    //Progress bar added to navigationBar
    CGFloat progressBarHeight = 2.0f;
    CGRect navigationBarBounds = self.navigationController.navigationBar.bounds;
    CGRect barFrame = CGRectMake(0, navigationBarBounds.size.height - progressBarHeight, navigationBarBounds.size.width, progressBarHeight);
    _mProgressView = [[UIProgressView alloc] initWithFrame:barFrame];
    _mProgressView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
    _mProgressView.progressTintColor = [UIColor greenColor];
    [self.navigationController.navigationBar addSubview:_mProgressView];
}

#Monitoring agent of pragma mark KVO
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {

    //Load progress value
    if ([keyPath isEqualToString:@"estimatedProgress"]){
        if (object == self.mWebView){
            self.mProgressView.alpha = 1;
            [self.mProgressView setProgress:self.mWebView.estimatedProgress animated:YES];
            if(self.mWebView.estimatedProgress >= 1.0f)
            {
                [UIView animateWithDuration:0.5 animations:^{
                    self.mProgressView.alpha = 0;
                } completion:^(BOOL finished) {
                     [self.mProgressView setProgress:0.0f animated:NO];
                }];
            }
        }else{
            [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
        }
    }else if ([keyPath isEqualToString:@"title"]){//Page title
        if (object == self.mWebView){
            self.navigationItem.title = self.mWebView.title;
        }else{
            [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
        }
    }else{
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}

At this point, loading the title and progress values is complete, try.

Posted by woza_uk on Sun, 31 May 2020 01:59:07 -0700