原文:https://www.jianshu.com/p/a5183d8d4a65
H5:<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> </head> <body> <div style="margin-top: 100px"> <h1>Objective-C和JavaScript交互</h1> </div> <div> <input type="button" value="oc掉用js弹出提示->" οnclick="clickAction0(11)"> <input type="button" value="js调用oc->" οnclick="clickbtn()"> </div> <script type="text/javascript"> // js函数 function clickAction0(typyId) { alert(typyId) } function clickAction1(typyId) { alert(typyId) } function clickAction2(typyId) { alert(typyId); return 'hello'; } function clickbtn() { var tempValue = window.AndroidWebView.indexOfMap(); alert(tempValue); } </script> </body> </html>
- 导入依赖库
JavaScriptCore.framework -
#import <UIKit/UIKit.h>
-
#import <JavaScriptCore/JavaScriptCore.h>
-
@protocol JSObjcDelegate <JSExport> // AndroidWebView对象调用的JavaScript方法,必须声明!!!
-
- (int)indexOfMap;
-
@end
-
@interface ViewController : UIViewController <UIWebViewDelegate,JSObjcDelegate> @property (nonatomic, strong) JSContext *context;
-
@property (weak, nonatomic) IBOutlet UIWebView *webView;
-
@end
- 加载网页(此处本地)
-
- (void)viewDidLoad {
-
[super viewDidLoad];
-
self.webView.delegate = self;
-
self.webView.scalesPageToFit = YES;//自动对页面进行缩放以适应屏幕 // 加载本地的html测试js
-
NSURL *baseURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]; NSString *path = [[NSBundle mainBundle] pathForResource:@"testJsFunc" ofType:@"html"];
-
NSString *html = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
-
[self.webView loadHTMLString:html baseURL:baseURL]; // Do any additional setup after loading the view, typically from a nib.
-
}
-
实现交互
-
- (void)webViewDidFinishLoad:(UIWebView *)webView {
-
// 获取context对象
-
self.context = [self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"]; //将AndroidWebView对象指向自身 js里面写window.AndroidWebView.indexOfMap() 就会调用原生里的indexOfMap方法
-
self.context[@"AndroidWebView"] = self;
-
self.context.exceptionHandler = ^(JSContext *context, JSValue *exceptionValue) { context.exception = exceptionValue;
-
NSLog(@"异常信息:%@", exceptionValue); };
-
// 获取到点击js按钮的事件 self.context[@"clickAction0"] = ^(){
-
NSLog(@"获取到点击js按钮的事件"); };
-
// oc调用js函数 并传参 js无返回值
-
NSString *jsAction = @"clickAction1(555)";
-
[self.context evaluateScript:jsAction];
-
// oc调用js函数 并传参 接收js返回值
-
NSString *str1 = [webView stringByEvaluatingJavaScriptFromString:@"clickAction2(666);"];
-
NSLog(@"js函数给我的返回值:%@", str1); } /** 待js调用 */
-
- (int)indexOfMap {
-
NSLog(@"我被js调用了");
-
return 110;
-
}
关于怎么接受JS 传过来的参数,事件已经捕获到了
// 获取到点击js按钮的事件
self.jsContext[@"clickAction"] = ^(){
[DLHelper showHud:@""];
NSArray *args = [JSContext currentArguments];
for (id obj in args) {
//JS 传过来的参数
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",obj]];
UIImageView *tempIV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 0.001, 0.001)];
[ws.view addSubview:tempIV];
[tempIV sd_setImageWithURL:url completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
if (image) {
// 保存图片
[tempIV removeFromSuperview];
UIImageWriteToSavedPhotosAlbum(image, ws, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}else{
[DLHelper toast:Localized(@"En_SaveFail")];
}
}];
}