iOS-WKWebview 帶有進度條載入的ViewController【KVO監聽Webview載入進度】,

來源:互聯網
上載者:User

iOS-WKWebview 帶有進度條載入的ViewController【KVO監聽Webview載入進度】,

 

前言

為什麼要說 WKWebview,在之前做電子書筆記時已經提過 WKWebview 在iOS8之後已完全替代 Webview,原因就不多說了,主要還是記憶體過大;

封裝

封裝一個基於 UIViewController 類: WKWebViewController

WKWebViewController.h

@interface WKWebViewController : UIViewController///目標URL@property (nonatomic,strong) NSString *url;@property (nonatomic,strong) WKWebView *webview;@end

WKWebViewController.m

#import "WKWebViewController.h"@interface WKWebViewController ()<WKNavigationDelegate,UIScrollViewDelegate>///進度條@property (nonatomic, strong) UIProgressView *progressView;@end@implementation WKWebViewController- (void)viewDidLoad {    [super viewDidLoad];    ///添加 KVO 對進度監聽    [self.webview addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];    [self.view addSubview:self.webview];    [self.view addSubview:self.progressView];    }#pragma mark - KVO- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {    if ([keyPath isEqualToString:@"estimatedProgress"]) {        self.progressView.progress = self.webview.estimatedProgress;        if (self.progressView.progress == 1) {            __weak typeof (self)weakSelf = self;            [UIView animateWithDuration:0.25f delay:0.3f options:UIViewAnimationOptionCurveEaseOut animations:^{                weakSelf.progressView.transform = CGAffineTransformMakeScale(1.0f, 1.4f);            } completion:^(BOOL finished) {                weakSelf.progressView.hidden = YES;                            }];        }    }else{        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];    }}#pragma mark - webview 代理//開始載入- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {    NSLog(@"開始載入網頁");    //開始載入網頁時展示出progressView    self.progressView.hidden = NO;    //開始載入網頁的時候將progressView的Height恢複為1.5倍    self.progressView.transform = CGAffineTransformMakeScale(1.0f, 1.5f);    //防止progressView被網頁擋住    [self.view bringSubviewToFront:self.progressView];}//載入完成- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {    NSLog(@"載入完成");    //載入完成隱藏progressView    self.progressView.hidden = YES;}//載入失敗- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error {    NSLog(@"載入失敗");    //載入失敗隱藏progressView    self.progressView.hidden = YES;}- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{    return nil;}#pragma mark - 屬性- (WKWebView *)webview{    if (!_webview) {        WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];        // 自適應螢幕寬度js        NSString *jSString = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";        WKUserScript *wkUserScript = [[WKUserScript alloc] initWithSource:jSString injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];                // 添加自適應螢幕寬度js調用的方法        [wkWebConfig.userContentController addUserScript:wkUserScript];        _webview = [[WKWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - (wkj_IsIphoneX ? 88 + 33 : 64)) configuration:wkWebConfig];        _webview.navigationDelegate = self;        _webview.scrollView.delegate = self;        _webview.opaque = NO;    }    return _webview;}- (UIProgressView *)progressView{    if (!_progressView) {        _progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, 2)];        _progressView.trackTintColor = [ColorKLSystem colorWithAlphaComponent:0.5];        _progressView.tintColor = ColorKLSystem;        _progressView.transform = CGAffineTransformMakeScale(1.0f, 1.5f);    }    return _progressView;}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.