IOS-WKWebview ViewController with progress bar loading [KVO listens to the Webview loading progress ],
Preface
Why do we need to say that WKWebview has already mentioned that WKWebview has completely replaced Webview after ios8. the reason is that the memory is too large;
Encapsulation
Encapsulate a Class Based on UIViewController: WKWebViewController
WKWebViewController. h
@ Interface WKWebViewController: UIViewController // target URL @ property (nonatomic, strong) NSString * url; @ property (nonatomic, strong) WKWebView * webview; @ end
WKWebViewController. m
# Import "WKWebViewController. h "@ interface WKWebViewController () <WKNavigationDelegate, progress> // progress bar @ property (nonatomic, strong) UIProgressView * progressView; @ end @ implementation WKWebViewController-(void) viewDidLoad {[super viewDidLoad]; // Add KVO to listen to the progress [self. webview addObserver: self forKeyPath: @ "estimatedProgress" options: NSKeyValueObservingOptionNew context: nil]; [self. view addSub View: 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 ispath tostring: @ "estimatedProgress"]) {self. progressView. progress = self. webview. estimatedProgress; if (self. progressView. progress = 1) {_ weak typeof (self) wea KSelf = 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 proxy // start loading-(void) web View :( WKWebView *) webView didStartProvisionalNavigation :( WKNavigation *) navigation {NSLog (@ ""); // The progressView self is displayed when the page is loaded. progressView. hidden = NO; // restore the Height of progressView to 1.5 times self when loading the webpage. progressView. transform = CGAffineTransformMakeScale (1.0f, 1.5f); // prevent progressView from being blocked by the webpage [self. view bringSubviewToFront: self. progressView];} // Load completed-(void) webView :( WKWebView *) webView didFinishNavigat Ion :( WKNavigation *) navigation {NSLog (@ ""); // hide progressView self after loading. progressView. hidden = YES;} // loading failed-(void) webView :( WKWebView *) webView didFailProvisionalNavigation :( WKNavigation *) navigation withError :( NSError *) error {NSLog (@ "loading failed"); // hide progressView self. progressView. hidden = YES;}-(UIView *) viewForZoomingInScrollView :( UIScrollView *) scrollView {return nil;} # pragma mark-Attribute-(WK WebView *) webview {if (! _ Webview) {WKWebViewConfiguration * wkWebConfig = [[WKWebViewConfiguration alloc] init]; // adaptive screen width 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 injec TionTime: WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly: YES]; // Add an adaptive screen width. js call method [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 = transform (1.0f, 1.5f);} return _ progressView;}-(void) didreceivemorywarning {[super didreceivemorywarning]; // Dispose of any resources that can be recreated .} @ end