Using code: You can paste directly into your project
. h
#import"BaseViewController.h"@interface LinkNewsController : BaseViewController/** 新闻链接 */@property (nonatomic, strong) NSString *link;@end
. m
#import <WebKit/WebKit.h> #import "MBProgressHUD.h" @interface linknewscontroller () <wknavigationdelegate>{ //MasksMbprogresshud *_MB;}//Page view@property(nonatomic,Strong) Wkwebview *wkwebview;@end @implementation linknewscontroller #pragma mark-Lazy loading-(Wkwebview *) wkwebview{if(_wkwebview = =Nil{_wkwebview = [[Wkwebview alloc] Initwithframe:cgrectmake (0, -, Kscreenwidth, kscreenheight- -)];NSLog(@"web link-->link:%@", Self. Link);nsurlrequest*request = [nsurlrequestrequestwithurl:[NsurlURLWithString: Self. Link]]; [_wkwebview setnavigationdelegate: Self]; [_wkwebview Loadrequest:request]; }return_wkwebview;}#pragma mark-View loading- (void) Viewdidload {[SuperViewdidload];additional setup after loading the view.[ Self. ViewAddsubview: Self. Wkwebview];}#pragma mark-wknavigationdelegate//Start loading-(void) WebView: (Wkwebview *) WebView didstartprovisionalnavigation: (wknavigation *) navigation{NSLog(@"Didstartprovisionalnavigation"); _MB = [Mbprogresshud showhudaddedto: Self. Navigationcontroller. ViewAnimated:YES]; [_mb Setmode:mbprogresshudmodeindeterminate]; [_mb setlabeltext:@"Loading ..."];}//onboarding Success-(void) WebView: (Wkwebview *) WebView didfinishnavigation: (wknavigation *) navigation{NSLog(@"didfinishnavigation__"); [_MB Hide:YES];}//Load Failure-(void) WebView: (Wkwebview *) WebView didfailnavigation: (wknavigation *) Navigation Witherror: (Nserror*) error{NSLog(@""); [_MB Hide:YES];}
The difference between UIWebView and Wkwebview
Wkwebview is faster (taking up memory may only have UIWebView 1/3~1/4), without caching. More carefully split the methods in the Uiwebviewdelegate.
The Wkwebview is a lightweight uiwebview.
Ios-wkwebview use