JS与OC互相调用Cordova详解+实战

xiaoxiao2025-08-31  10

https://www.jianshu.com/p/e74bc7abac8d

 

 

oc to js

NSString *str = [webView stringByEvaluatingJavaScriptFromString:@"show('sssff')"]; // 或 JSContext *context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"]; NSString *textJS = @"show('sssff')"; [context evaluateScript:textJS];

js to oc

#import <JavaScriptCore/JavaScriptCore.h> JSContext *context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];     context[@"scan"] = ^() {         NSLog(@"scan");     };

 

html js

<!DOCTYPE html> <head> <script type="text/javascript"> function show(str){ return str; } function calloc(){ //window.location.href= "vv://vv.com" scan() } </script> </head> <body> <input type="button" value="JS调用OC方法" onclick="calloc()"/> </body> </html>

 

转载请注明原文地址: https://www.6miu.com/read-5035523.html

最新回复(0)