Wkwebview is used in the project, and the following are extracted from the project for friends who want to use Wkwebview.
The controller class name and the method name in the class can be modified directly in the code, and can be used more quickly in the project.
(a). h definition File
#import <UIKit/UIKit.h> @interface Hzwebviewcontroller:uiviewcontroller@end
(b). m implementation file
#import "HZWebViewController.h" #import <WebKit/WebKit.h> @interface Hzwebviewcontroller () < Wkscriptmessagehandler,wknavigationdelegate,wkuidelegate> @property (nonatomic,strong) WKWebView *webView;@ End@implementation hzwebviewcontroller-(void) viewdidload {[Super viewdidload]; Do any additional setup after loading the view. Basic configuration [self commoninit]; Load URL (the actual address in the project), here with "www.baidu.com" example NSString *urlstring = @ "www.baidu.com"; Nsurlrequest *request = [[Nsurlrequest alloc] Initwithurl:[nsurl urlwithstring:urlstring] CachePolicy: ( Nsurlrequestuseprotocolcachepolicy) TIMEOUTINTERVAL:16.F]; [Self.webview Loadrequest:request]; Register a notification so that you can handle the business, for example, after you select a coupon on the coupon page, you can pass it to the H5 page [[Nsnotificationcenter Defaultcenter] addobserver:self selector:@ Selector (valuetransmit:) name:@ "Selectedcouponnotification" Object:nil];} -(void) Valuetransmit: (nsnotification *) Notification {nsdictionary *dict = Notification.userinfo; ID Object = NotiFication.object; /*///Coupon page selected coupon Array (contains is coupon model Hzcouponmodel) Nsarray *couponarray = Notification.object; Model array, dictionary array nsarray *dictarray = [Hzcouponmodel Mj_keyvaluesarraywithobjectarray:couponarray]; The dictionary array->json, and H5 is interactive through the Json data format nsstring *str = [Dictarray tojsonstring]; Backorderweb is a H5 defined method nsstring *js = [NSString stringwithformat:@ "Backorderweb (%@)", str]; *///JS code can be passed an array containing multiple models, etc. nsstring *JS = @ ""; App->h5 value [Self.webview evaluatejavascript:js completionhandler:^ (ID object, Nserror *error) {if (error) {//Logical processing}else {NSLog (@ "%@", error); } }];} -(void) Commoninit {//config parameter wkwebviewconfiguration *config = [[Wkwebviewconfiguration alloc]init]; Set Preferences config.preferences = [[Wkpreferences alloc]init]; The default is 0 config.preferences.minimumFontSize = 10; By default it is considered Yes CONFIG.PREFERENCES.JAVASCRiptenabled = YES; The default is no on iOS, indicating that config.preferences.javaScriptCanOpenWindowsAutomatically = YES is not automatically opened through the window; Web content processing pool, because there is no property can be set, there is no method can be called without manually creating Config.processpool = [[Wkprocesspool alloc]init]; Interact with WebView content via JS config.usercontentcontroller = [[Wkusercontentcontroller alloc] init]; Inject JS object name Appmodel, when JS through Appmodel to call,//We can receive in the Wkscriptmessagehandler agent//Couponcallback,orderidcallback,coupo Nsjumptopaydone is the method name agreed with the front end [Config.usercontentcontroller addscriptmessagehandler:self name:@ "CouponCallBack"]; [Config.usercontentcontroller addscriptmessagehandler:self name:@ "Orderidcallback"]; [Config.usercontentcontroller addscriptmessagehandler:self name:@ "Couponsjumptopaydone"]; Wkwebview *webview = [[Wkwebview alloc] InitWithFrame:self.view.bounds configuration:config ]; Webview.backgroundcolor = [Uicolor Clearcolor]; Webview.opaque = YES; WebView.scrollView.showsVerticalScrollIndicator = NO; WebView.scrollView.showsHorizontalScrollIndicator = NO; Webview.navigationdelegate = self; Webview.uidelegate = self; Self.webview = WebView; [Self.view Addsubview:webview];} #pragma mark-wknavigationdelegate/* * Before sending a request, decide whether to jump */-(void) WebView: (Wkwebview *) WebView Decidepolicyfornavigationaction: (wknavigationaction *) navigationaction Decisionhandler: (void (^) ( Wknavigationactionpolicy)) Decisionhandler {nsstring *urlstring = [navigationAction.request.URL.absoluteString stri Ngbyremovingpercentencoding]; if ([[[URLString lastpathcomponent] isequaltostring:@ "Opencoupon"]) {//Open coupon page Decisionhandler (wknavigation Actionpolicycancel); } else if ([[[URLString lastpathcomponent] isequaltostring:@ "Backprodutiondetail"]) {//Return to Product details Decisionhandl ER (wknavigationactionpolicycaNCEL); } else {Decisionhandler (wknavigationactionpolicyallow); }}/* * After the page load is completed call */-(void) WebView: (Wkwebview *) WebView didfinishnavigation: (wknavigation *) Navigation {NSLog (@ "page finished loading ");} /* * Page load failed when calling */-(void) WebView: (Wkwebview *) WebView didfailprovisionalnavigation: (wknavigation *) Navigation {NSLog (@ "Page load Failed");} /* * When content starts to return, call */-(void) WebView: (Wkwebview *) WebView didcommitnavigation: (wknavigation *) Navigation {}/* * start navigation jump back */-(void) WebView: (Wkwebview *) WebView didstartprovisionalnavigation: (wknavigation *) Navigation {NSLog (@ "Come to this method");} /* * */-(void) Evaluatejavascript is called when executing the JS code: (NSString *) javascriptstring Completionhandler: (void (^) (ID, Nserror *)) Completionhandler {}/* * H5->app value * Injected JS object name Appmodel, when JS is called through Appmodel, * received Wkscriptmessagehandler in */-agent ( void) Usercontentcontroller: (Wkusercontentcontroller *) Usercontentcontroller didreceivescriptmessage: ( Wkscriptmessage *) message{nsstring *messagename = message.name;ID messagebody = message.body; Logical processing can be done in the corresponding statements, such as jumping to the corresponding interface if ([MessageName isequaltostring:@ "Couponcallback"]) {}else if ([MessageName I sequaltostring:@ "Orderidcallback"]) {}else if ([MessageName isequaltostring:@ "Couponsjumptopaydone"]) { }} #pragma mark-wkuidelegate/** * @author chencheng * * Web interface has a popup when called * * @param webView Implementation of the proxy WebView * @param the contents of the message popup * @param frame main window * @param completionhandler pop-up box disappears call */-(void) WebView: (W Kwebview *) WebView runjavascriptalertpanelwithmessage: (NSString *) message initiatedbyframe: (Wkframeinfo *) frame Completionhandler: (void (^) (void)) Completionhandler {uialertaction *action = [uialertaction actionwithtitle:@ "OK" Style:uialertactionstyledefault handler:^ (uialertaction * _nonnull action) {if (action) {Completionhan Dler (); } }]; Uialertcontroller *alertcontroller = [[Uialertcontroller alloc] init]; [AlertController addaction:action];} /** * The Web interface has a popup warning box when called * * @param webView Implement the agent's WebView * @param message warning Box contents * @param frame Main Window * @param completionhandler warning box disappears call */-(void) WebView: (Wkwebview *) WebView Runjavascriptconfirmpanelwithmessa GE: (NSString *) message initiatedbyframe: (Wkframeinfo *) frame Completionhandler: (void (^) (BOOL)) Completionhandler { Uialertaction *cancelaction = [uialertaction actionwithtitle:@ "OK" Style:uialertactionstyledefault handler:^ ( Uialertaction * _nonnull action) {if (action) {Completionhandler (NO); } }]; Uialertaction *action = [uialertaction actionwithtitle:@ "OK" style:uialertactionstyledefault handler:^ (UIAlertAction * _nonnull action) {if (action) {Completionhandler (YES); } }]; Uialertcontroller *alertcontroller = [[Uialertcontroller alloc] init]; [Alertcontroller addaction:cancelaction]; [Alertcontroller addaction:action];} -(void) webview: (Wkwebview *) WebView runjavascripttextinputpanelwithprompt: (NSString *) prompt DefaultText: (NSString *) DefaultText initiatedbyframe: (Wkframeinfo *) frame Completionhandler: (void (^) (NSString *)) Completionhandler {} @end
Use of IOS Wkwebview