Implementation of WKScriptMessageHandler, convention interaction Handler
Apple requires that newly released apps not use UIWebView. Interaction only summarizes WKWebView. First of all, specify the name of ScriptMessageHandler, such as lvJSCallNativeHandler
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message;
Copy the code
Use the Develop feature in Safari to debug the built-in H5 page. In the console input window. Its. MessageHandlers. LvJSCallNativeHandler, can add the Handler object hit native.
Convention UserAgent
Set customUserAgent so that H5 can distinguish between operating environment, whether it is in App, version, device model, etc. Just have it when you need it.
Title and load progress monitoring
- As a result of WKWebview
estimatedProgress
Property is kVO-enabled to enable the progressView to show loading progress. There is usually a default value, such as 0.15, for a better experience to give the user the illusion of fast loading. title
Property is also KVO-enabled, and the title of the native navigation bar can be changed synchronously
Specify notification methods at various points in the App lifecycle
For example UIViewController viewWillAppear:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.webView evaluateJavaScript:@"window.viewWillAppear && viewWillAppear();" completionHandler:nil];
}
Copy the code
Native events | Call the js method and notify H5 |
---|---|
viewWillAppear | viewWillAppear() |
viewWillDisappear | viewWillDisappear() |
UIApplicationWillEnterForegroundNotification | willEnterForeground() |
Native back button | lvNativeBackCheck() |
Native login successful | loginOnsuccess() |
Native login failure | loginOnfail() |
Login Status Through
Native login status should be available in H5. For example, if H5 uses the lvsessionID in the Cookie to determine login status, then write the lvsessionID into the Cookie. Note the domain name
NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:
@{NSHTTPCookieDomain: @".example.com".NSHTTPCookiePath: @"/".NSHTTPCookieName: @"lvsessionid".NSHTTPCookieValue: lvsessionid}];
WKHTTPCookieStore *cookieStore = self.webView.configuration.websiteDataStore.httpCookieStore;
[cookieStore setCookie:cookie completionHandler:nil];
Copy the code
Common convention methods, arguments are dictionaries, return values are dictionaries
- Loading lvJSShowLoading; Hidden Loading lvJSHideLoading
- Get the location lvJSGetLocation
- Turn on the camera and scan the QR code
- Opening address book lvJSOpenContacts without permission returns an error
- Information stored in the read NSUserDefault lvJSGetUserDefaultWithKey; Set the lvJSSetUserDefaultWithKey information stored in the local
- Page jump lvJSGoClass, according to the parameter className class reflection to determine which page to jump
- Browse history lvJSSaveHistory
- Gets the screen size and status bar height lvJSGetScreenSize
- Go to iOS APP Settings page lvJSGoAppSettingPage
- Hide the navigation bar lvJSHideNativeNavigationBar; Show the navigation bar lvJSShowNativeNavigationBar
- Sets whether the webView can bounce back to lvJSSetWebViewBounces
- Copy the text to the clipboard lvJSCopyToPasteboard
Other Special Provisions
- An exception convention for the App to intercept the URL of a particular rule to the native page.
For example: ticket details URL, piAO after the product ID.
https://m.example.com/ticket/piao-983253
Copy the code
But some ids cannot be intercepted. This requires an exception rule, with the requireh5=1 parameter
https://m.example.com/ticket/piao-983253?requireh5=1
Copy the code
The App will not intercept if it contains requireh5=1. I’m just going to open it up with the WebView
- Whether the WebView is open more (open in the new Controller when clicking the link, not refresh in the current Controller)
WebView makes HybirdApp more native. Newtab =1 note that the native implementation uses copy and passes request (do not pass the URL, some of the request information will be lost). You can also specify multiple presentation methods, such as push or present