最近遇到一个Object-C与JavaScript相互调用的问题处理方式如下:
注意:一定要写对H5中的方法名
举例说明:
- ViewController.h文件
#import <UIKit/UIKit.h>
#import <JavaScriptCore/JavaScriptCore.h>
@protocol JSObjcDelegate <JSExport>
//对象调用的JavaScript方法,必须声明!!!
- (void)call1;
- (void)getCall:(NSString *)callString;
@end
@interface ViewController : UIViewController<UIWebViewDelegate,JSObjcDelegate>
@property (nonatomic, strong) JSContext *jsContext;
@property (strong, nonatomic) UIWebView *webView;
@end
- ViewController.m文件
#import "ViewController.h"
@interface ViewController ()
@property(nonatomic,copy)NSString * JSName;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 20, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
self.webView.delegate = self;
//从本地加载html文件
NSString* path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
NSURL* url = [NSURL fileURLWithPath:path];
NSURLRequest* request = [NSURLRequest requestWithURL:url] ;
[self.webView loadRequest:request];
[self.view addSubview:self.webView];
}
//JavaScript的tianbai是一个对象,充当原生应用和web页面之间的一个桥梁。用来调用方法
//webview加载完成调用代理
- (void)webViewDidFinishLoad:(UIWebView *)webView {
// 设置javaScriptContext上下文
self.jsContext = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
//将tianbai对象指向自身
self.jsContext[@"baoming"] = self;
self.jsContext.exceptionHandler = ^(JSContext *context, JSValue *exceptionValue) {
context.exception = exceptionValue;
NSLog(@"异常信息:%@", exceptionValue);
};
}
//将对象指向自身后,如果调用 tianbai.call() 会响应下面的方法,OC方法中调用js中的Callback方法,并传值
- (void)call1{
NSString * str = self.JSName;
JSValue *openLogin = self.jsContext[str];
//获取包名
NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
NSString * bildName = [infoDictionary objectForKey:@"CFBundleDisplayName"];
//传值给web端
[openLogin callWithArguments:@[bildName]];
}
//将对象指向自身后,如果调用 tianbai.getCall(callInfo) 会响应下面的方法,OC方法中仅调用JavaScript中的alerCallback方法
- (void)getCall:(NSString *)callString{
self.JSName = callString;
[self call1];
NSLog(@"Get:%@", callString);
// 成功回调JavaScript的方法Callback
// JSValue *Callback = self.jsContext[@"alerCallback"];
// [Callback callWithArguments:nil];
}
//将对象指向自身后,还可以向html注入js
- (void)alert{
// 直接添加提示框
NSString *str = @"alert('OC添加JS提示成功')";
[self.jsContext evaluateScript:str];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
3.HTML文件
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<div style="margin-top: 20px">
<h2>JS与OC交互</h2>
<input type="button" value="唤起本地方法(call)" onclick="baoming.call()">
</div>
<div>
<input type="button" value="唤起getCall:(NSString *)callString传值" onclick="call()">
</div>
<div>
<input type="button" value="包名" onclick="call1()">
</div>
<script>
var call1 = function()
{
var callInfo = {"jianshu": "http://www.jianshu.com/users/55c8fdc3c6e7/latest_articles", "ceshi": "data111"};
tianbai.getCall(callInfo);
}
var Callback = function(str)
{
alert(str);
}
var openLogin = function(str)
{
alert(str);
}
var alerCallback = function()
{
alert('成功');
}
</script>
</body>
</html>