The h5 end:
import Vue from "vue";
import utils from "@/assets/js/utils";
import { Toast } from "vant";
Vue.use(Toast);
function callApp(name, androidData, iosData) {
if (utils.judgeBrowser().android) {
if (window.JSCallJava) {
if (window.JSCallJava[name]) {
if (androidData) {
window.JSCallJava[name](androidData);
} else{ window.JSCallJava[name](); }}}}else if (utils.judgeBrowser().ios) {
if (window.webkit && window.webkit.messageHandlers) {
if (window.webkit.messageHandlers[name]) {
if (iosData) {
window.webkit.messageHandlers[name].postMessage(iosData);
} else{ window.webkit.messageHandlers[name].postMessage(); }}}}} // Specifies the method nameexport function closePage() {
callApp("closePage".""."1");
}
Copy the code
The iOS side
#import "JNDelegateCommonWebView.h"
#import <WebKit/WebKit.h>@interface JNDelegateCommonWebView ()<WKNavigationDelegate, WKUIDelegate,WKScriptMessageHandler> @property (nonatomic, strong) WKWebView *web; @end @implementation JNDelegateCommonWebView - (void)viewDidLoad { [super viewDidLoad]; [self createUI]; } -(void)createUI{ WKWebViewConfiguration *config = [WKWebViewConfiguration new]; config.userContentController = [WKUserContentController new]; / / registered js method [config userContentController addScriptMessageHandler: self name: @"closePage"];
self.web = [[WKWebView alloc]initWithFrame:CGRectZero configuration:config];
_web.navigationDelegate = self;
[self.view addSubview:_web];
}
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
VLog(@"Method name :%@, data passed :%@",message.name,message.body); / / the method nameif ([message.name isEqualToString:@"closePage"]) {
[self.navigationController popViewControllerAnimated:YES];
}
}
@end
Copy the code