iOS 关于js与OC相互调用的那些事

时间:2023-03-10 01:17:19
iOS 关于js与OC相互调用的那些事

最近项目上使用js调用OC,OC再次调用JS,再次在JS页面上面回显数据。

项目中使用的是WKWebview,加载网路的URL,其实就是使用WK加载出来的H5网页,在项目中用的是H5网页有个识别按钮,调起工程里面的OCR识别功能进行识别,OCR识别的数据信息放到一个数据字典里面,直接回传值即可,用OC执行远程的js数据进行回显数据,把json字符串传给他,前端用js进行回显数据字典。

js调用OC,以WKWebview为例

WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];

userContentController = [WKUserContentController new];

//提供一个扫描的js

[userContentController addScriptMessageHandler:self name:@"OCRScanAction"];

WKPreferences *preferences = [WKPreferences new];

preferences.javaScriptCanOpenWindowsAutomatically=YES;

//        preferences.minimumFontSize = 40;

configuration.preferences =preferences;

configuration.userContentController = userContentController;

configuration.preferences.javaScriptEnabled = YES;

webview = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) configuration:configuration];

webview.allowsBackForwardNavigationGestures = YES;//打开网页间的滑动返回

webview.allowsLinkPreview = YES; //允许预览链接

webview.navigationDelegate = self;

webview.scrollView.delegate = self;

webview.UIDelegate = self;

[webview sizeToFit];

[self.view addSubview:webview];

webview.scrollView.scrollEnabled = YES;

NSURL *url = [NSURL URLWithString:urlString];

NSURLRequest *request = [NSURLRequest requestWithURL:url];

[webview loadRequest:request];

//js调用OC代码段实现OCR功能

- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message{

NSLog(@"%@",message);

NSLog(@"body:%@",message.body);

if ([message.name isEqualToString:@"OCRScanAction"]) {

//调用OCRSDK

NSLog(@"message.body==%@",[message.body objectForKey:@"_clicktype"]);

NSString *ocrType = [message.body objectForKey:@"_clicktype"];

[[NSUserDefaults standardUserDefaults] setObject:ocrType forKey:@"_clicktype"];

NSLog(@"启动OCR");

ViewController *vc = [[ViewController alloc] init];

[self presentViewController:vc animated:YES completion:nil];

}

}

ViewController    是原生的控制器进行OCR扫描,把识别的信息传值过来 发送一个通知

[[NSNotificationCenter defaultCenter] postNotificationName:@"ocr" object:self userInfo:dict];//dict为数据字典

[self dismissViewControllerAnimated:YES completion:nil];

在H5网页控制器做监听

//设置OCR识别的监听

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ocrBack:) name:@"ocr" object:nil];

#pragma mark---监听事件

- (void)ocrBack:(NSNotification *)notification{

NSDictionary *dict = notification.userInfo;

NSString *jsonStr = [self dictionaryToJson:dict];

NSString *jsStr = [NSString stringWithFormat:@"OCRScanActionCallback('%@')",jsonStr];

[webview evaluateJavaScript:jsStr completionHandler:^(id _Nullable result, NSError * _Nullable error) {

NSLog(@"%@----%@",result, error);

//        NSLog(@"%@",result);

}];

}

#pragma mark --字典转换成字符串

- (NSString*)dictionaryToJson:(NSDictionary *)dic

{

NSError *parseError = nil;

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options: error:&parseError];//注意这里options参数必须传0,否则回调回报语法错误

return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

}

//就这里能显示出来回显的数据字典  把OCR扫描的结果传给H5页面上赋值进行回显。