IOS-WKWebView usage
Code: You can directly paste it into your project.
. H
# Import "BaseViewController. h" @ interface LinkNewsController: BaseViewController/** news link */@ property (nonatomic, strong) NSString * link; @ end
. M
# Import
# Import "MBProgressHUD. h" @ interface LinkNewsController ()
{// Mask MBProgressHUD * _ MB;} // webpage view @ property (nonatomic, strong) WKWebView * WKWebView; @ end @ implementation LinkNewsController # pragma mark-lazy loading-(WKWebView *) WKWebView {if (_ WKWebView = nil) {_ WKWebView = [[WKWebView alloc] initWithFrame: CGRectMake (0, 64, kScreenWidth, kScreenHeight-64)]; NSLog (@ "webpage link --> link: % @", self. link); NSURLRequest * request = [NSURLRequest requestWithURL: [NSURL URLWithString: self. link]; [_ WKWebView setNavigationDelegate: self]; [_ WKWebView loadRequest: request];} return _ WKWebView;} # pragma mark-view loading-(void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view. [self. view addSubview: self. WKWebView] ;}# pragma mark-WKNavigationDelegate // start loading-(void) webView :( WKWebView *) webView progress :( WKNavigation *) navigation {NSLog (@ "didStartProvisionalNavigation "); _ MB = [MBProgressHUD showHUDAddedTo: self. navigationController. view animated: YES]; [_ MB setMode: MBProgressHUDModeIndeterminate]; [_ MB setLabelText: @ "loading... "];} // load succeeded-(void) webView :( WKWebView *) webView didFinishNavigation :( WKNavigation *) navigation {NSLog (@" didFinishNavigation _ "); [MBProgressHUD hideAllHUDsForView: self. navigationController. view animated: YES];} // loading failed-(void) webView :( WKWebView *) webView didFailNavigation :( WKNavigation *) navigation withError :( NSError *) error {NSLog (@ ""); [MBProgressHUD hideAllHUDsForView: self. navigationController. view animated: YES];}
Difference between UIWebView and WKWebView
WKWebView is faster (the memory usage may be only 1/3 of that of UIWebView ~ 1/4), no cache, more detailed splitting of methods in UIWebViewDelegate.
WKWebView is a lightweight UIWebView.