由于时间的关系,直接上代码
JSCallOC.html
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no, target-densitydpi=device-dpi"/>
<title>JSCallOC</title>
<style>
*
{
//-webkit-tap-highlight-color: rgba(0,0,0,0);
text-decoration: none;
}
html,body
{
-webkit-touch-callout: none; /* prevent callout to copy image, etc when tap to hold */
-webkit-text-size-adjust: none; /* prevent webkit from resizing text to fit */
-webkit-user-select: none; /* prevent copy paste, to allow, change 'none' to 'text' */
}
#div-a
{
background:#FBA;
color:#FFF;
border-radius: 25px 5px;
}
</style>
<script type="text/javascript">
function showResult(resultNumber) {
//alert(resultNumber);
document.getElementById("result").innerText = resultNumber;
}
</script>
</head>
<body style="background:#CDE; color:#FFF">
<!--<div>
<font size="3" color="black">输入一个整数:</font>
<textarea id="input" style="font-size:10pt;color:black;"></textarea>
</div>
<br/>
<div>
<font size="3" color="black">结果: <b id="result"> </b> </font>
</div>
<br/>-->
<div>
<font size="3" color="black"> <b id="result"> </b> </font>
</div>
<br/>
<div id="div-a">
<center>
<br/>
<input type="button" value="带2个入参的方法测试" οnclick="Native.testMethodWithParam1Param2('param1_value','param2_value');" />
<br/>
<br/>
<br/>
<input type="button" value="带2个入参的方法测试2" οnclick="Native.testMethod(11111,'22222');" />
<br/>
<br/>
<br/>
<input type="button" value="参数为数组的方法测试" οnclick="Native.testArray([11111,'22222']);" />
<br/>
<br/>
<input type="button" value="带一个参数的方法测试" οnclick="Native.testLog('测试');" />
<br/>
<br/>
<input type="button" value="测试在HTML页面显示数据的" οnclick="Native.testShowTextOnHtml('1234');" />
<br/>
<br/>
<!--<br/>
<input type="button" value="计算阶乘" οnclick="native.calculateForJS(input.value);" />
<br/>
<br/>
<input type="button" value="测试log" οnclick="log('测试');" />
<br/>
<br/>
<input type="button" value="oc原生Alert" οnclick="alert('alert');" />
<br/>
<br/>
<input type="button" value="addSubView" οnclick="addSubView('view');" />
<br/>
<br/>
<a id="push" href="#" οnclick="native.pushViewControllerTitle('SecondViewController','secondPushedFromJS');">
push to second ViewController
</a>
<br/>-->
<br/>
</center>
</div>
</body>
</html>
----------------------------------------------------
JSDemoViewController.h
#import <UIKit/UIKit.h>
#import <JavaScriptCore/JavaScriptCore.h>
@protocol JSDemoExport <JSExport>
//JSExportAs
//(calculateForJS /** handleFactorialCalculateWithNumber 作为js方法的别名 */,
// - (void)handleFactorialCalculateWithNumber:(NSNumber *)number
//
// );
//
//- (void)pushViewController:(NSString *)view title:(NSString *)title;
-(void)testMethodWithParam1:(NSString *)param1 param2:(NSString *)param2;
-(void)testLog:(NSString *)logText;
-(void)testShowTextOnHtml:(NSString *)showText;
-(void)test:(NSNumber *)param1 method:(NSString *)param2;
-(void)testArray:(NSArray *)dataArray;
@end
@interface JSDemoViewController : UIViewController <UIWebViewDelegate, JSDemoExport>
{
UIWebView *myWebView;
JSContext *context;
}
@end
========================================================
#import "JSDemoViewController.h"
@interfaceJSDemoViewController ()
@end
@implementation JSDemoViewController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view.
NSString *path = [[[NSBundlemainBundle] bundlePath] stringByAppendingPathComponent:@"JSCallOC.html"];
// NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]];
// [self.webView loadRequest:request];
//NSLog(@"path >>>= %@",path);
myWebView = [[UIWebViewalloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 400)];
myWebView.delegate = self;
NSURL *URL = [NSURL URLWithString:path];
NSURLRequest *requestww = [NSURLRequest requestWithURL:URL];
[myWebView loadRequest:requestww];
[self.view addSubview:myWebView];
//NSLog(@"webView.frame = %@",NSStringFromCGRect(webView.frame));
}
#pragma mark - UIWebViewDelegate
- (void)webViewDidFinishLoad:(UIWebView *)webView{
// 以 html title 设置 导航栏 title
self.title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
// 禁用 页面元素选择
//[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitUserSelect='none';"];
// 禁用 长按弹出ActionSheet
//[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitTouchCallout='none';"];
// Undocumented access to UIWebView's JSContext
context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
// 打印异常
context.exceptionHandler = ^(JSContext *context, JSValue *exceptionValue) {
context.exception = exceptionValue;
NSLog(@"%@", exceptionValue);
};
context[@"Native"] = self; //以JSExport 协议关联 native 的方法
context[@"log"] = ^(NSString *str){//以block 形式关联 JavaScript function
NSLog(@"%@", str);
};
//
// 以 block 形式关联 JavaScript function
context[@"alert"] = ^(NSString *str) {
UIAlertView *alert = [[UIAlertViewalloc]initWithTitle:@"msg from js"message:str delegate:nilcancelButtonTitle:@"ok"otherButtonTitles:nil, nil];
[alert show];
};
//
__blocktypeof(self) weakSelf = self;
context[@"addSubView"] = ^(NSString *viewname) {
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(10, 500, 300, 100)];
view.backgroundColor = [UIColorredColor];
UISwitch *sw = [[UISwitch alloc]init];
[view addSubview:sw];
[weakSelf.view addSubview:view];
};
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)testArray:(NSArray *)dataArray{
NSLog(@"testArray = %@", dataArray);
}
-(void)test:(NSNumber *)param1 method:(NSString *)param2{
NSLog(@"test>>> param1 = %@, method=%@ ",param1, param2);
}
-(void)testMethodWithParam1:(NSString *)param1 param2:(NSString *)param2{
NSLog(@"testMethodWithParam1>>> param1 = %@, param2=%@ ",param1, param2);
}
-(void)testLog:(NSString *)logText{
NSLog(@"testLog>>> logText = %@ ", logText);
}
-(void)testShowTextOnHtml:(NSString *)showText{
NSString *resultText = [NSString stringWithFormat:@"%@ date=%@",showText, [NSDate date]];
NSLog(@"%@", resultText);
[context[@"showResult"] callWithArguments:@[resultText]];//回调JS的方法showResult(resultText);
}
@end