由于时间的关系,直接上代码
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个入参的方法测试" onclick="Native.testMethodWithParam1Param2('param1_value','param2_value');" />
<br/>
<br/>
<br/>
<input type="button" value="带2个入参的方法测试2" onclick="Native.testMethod(11111,'22222');" />
<br/>
<br/>
<br/>
<input type="button" value="参数为数组的方法测试" onclick="Native.testArray([11111,'22222']);" />
<br/>
<br/>
<input type="button" value="带一个参数的方法测试" onclick="Native.testLog('测试');" />
<br/>
<br/>
<input type="button" value="测试在HTML页面显示数据的" onclick="Native.testShowTextOnHtml('1234');" />
<br/>
<br/>
<!--<br/>
<input type="button" value="计算阶乘" onclick="native.calculateForJS(input.value);" />
<br/>
<br/>
<input type="button" value="测试log" onclick="log('测试');" />
<br/>
<br/>
<input type="button" value="oc原生Alert" onclick="alert('alert');" />
<br/>
<br/>
<input type="button" value="addSubView" onclick="addSubView('view');" />
<br/>
<br/>
<a id="push" href="#" onclick="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
相关推荐
iOS JavaScriptCore 演示 这是一个演示 iOS 应用程序,展示了如何从 Objective-C 运行 JavaScript 函数。 如果您正在阅读本文,您可能还库的感兴趣。 截屏
iOS javascript engine: Javascriptcore for iOS Demo project
该文件李含有三个文件夹,分别是iOS7之前的旧方法实现的JS与OC之间的混合开发、iOS7之后的javascriptCore.framework框架、GitHub上的demo。
IOS7之前,接触 JS 更多的是处理UIWebView的时候,如:...但IOS7引入了JS框架<JavaScriptCore/JavaScriptCore.h>,给了“纯IOS程序员”一个枯木逢春的契机~学习强大的 JavaScript。
ios7之后,ios中加入了JavaScriptCore框架。该框架让Objective-C和JavaScript代码直接的交互变得更加的简单方便。ios 原生与js交互,demo代码,其中包括利用JSBinding简单使用!
JavaScript 与 iOS 交互 ,采用iOS 7之后 自带 JavaScriptCore实现 ,使用简单无需第三方库 Demo地址:https://github.com/GesanTung/JS2iOS
iOS7后苹果在iPhone平台推出,极大的方便了我们对js的操作。 首先创建webView,读取本地的html文件 NSURL* htmlURL = [[NSBundle mainBundle] URLForResource: @"demo" withExtension: @"html"]; [_webView ...
将 REPL集成到iOS,macOS和tvOS上的嵌入式JavaScriptCore中。 Ambly是设计用于开发混合ClojureScript /本机应用程序时使用的REPL。 Ambly包含ClojureScript REPL实现以及与JavaScriptCore接口的Objective-C代码。 ...
最近公司需要写个交互,写的看了看,用JavaScriptCore写的,做完发现挺简单的,因为是demo,写的比较low,有注释 有需要的就快来看看吧,但是发现如果是跳转的话交互就会失效,知道怎么解决这个问题的大牛还望...
JSPatch是GitHub上一个开源的框架,其可以通过Objective-C的run-time机制动态的使用JavaScript调用与替换项目中的Objective-C属性与方法。其框架小巧,代码简洁,并且通过系统的JavaScriptCore框架与Objective-C进行...
note-on-iOS-work-with-JavaScriptiOS 和 JavaScript 交互的几种方式 demo, 相关 Blog Post:
是一个苹果在iOS7引入的框架,该框架让 Objective-C 和 JavaScript 代码直接的交互变得更加的简单方便。 JavaScriptCore 是苹果Safari浏览器JavaScript引擎,或许你听过Google的V8引擎,在WWDC上苹果演示了最新的...
HTML5 Canvas 2D 和 WebGL API 使用iOS原生的 OpenGL 来实现的。而 Audio 是用 OpenAL 实现的. 其他的一些 APIs (比如touch,accelerometer,localStorage等等)和在真正的浏览器中表现效果类似。 具体用法请看...