objectc 与 js 交互
2015-06-09 16:27
453 查看
Execute Javascript in iOS Applications
In this tutorial, I have covered How to execute JavaScript in iOS / Objective-C. You can execute JavaScript in iOS applications with the help of JavaScriptCore.framework. This framework is supported in iOS7 & OSX 10.9
List of features supported by Javascript.framework
a) Evaluate JavaScript code in Objective-C
b) Access JavaScript Variables in Objective-C
c) Access JavaScript functions and execute them in Objective-C
d) Execute Objective-C code from JavaScript
e) Export Objective-C classes and use them in JavaScript.
Download the source code:
To access JavaScript framework, you need to include the header file
List of Main Classes supported by framework:
JSContext : For any javascript operations you need JSContext.
JSValue : JSValue holds JavaScript values,variables and functions. List of main methods in JSValue.
1).Evaluating Javascript Code
2).Access Javascript Variables. Variables and functions are accessed from JSContext.
To access varible from Javascript : context[@"x"]
To access function from javascript : context[@"myfun"]
3).Access Javascript functions and execute them in Objective-C
Objective-C code is called from JavaScript in two ways
Objective-C code block can be called from JavaScript.
You can see the comparison between Objective-C Object and it’s equivalent Javascript Object below.
To make Javascript interacting with Objective-C Object, We need to have a Protocol which should be inherited
from JSExport. Move all the variables and functions to prototype to access them in JavaScript.
Now MyObject(Objective-C)’s variables and functions are directly accessed from javascript.
In this tutorial, I have covered How to execute JavaScript in iOS / Objective-C. You can execute JavaScript in iOS applications with the help of JavaScriptCore.framework. This framework is supported in iOS7 & OSX 10.9
List of features supported by Javascript.framework
a) Evaluate JavaScript code in Objective-C
b) Access JavaScript Variables in Objective-C
c) Access JavaScript functions and execute them in Objective-C
d) Execute Objective-C code from JavaScript
e) Export Objective-C classes and use them in JavaScript.
Download the source code:
To access JavaScript framework, you need to include the header file
JSContext : For any javascript operations you need JSContext.
Accessing Javascript Code in Objective-C
1).Evaluating Javascript CodeTo access varible from Javascript : context[@"x"]
To access function from javascript : context[@"myfun"]
Call Objective-C code from JavaScript
Objective-C code is called from JavaScript in two ways
1.CODE BLOCKS
Objective-C code block can be called from JavaScript.
2.USING JSEXPORT PROTOCOL
You can see the comparison between Objective-C Object and it’s equivalent Javascript Object below.from JSExport. Move all the variables and functions to prototype to access them in JavaScript.
相关文章推荐
- 《Effective Objective-C》读书笔记(2)
- Object-C-NSFileHandle
- Objective C静态代码扫描和代码质量管理 OClint + SonarQube
- 《Effective Objective-C》读书笔记(1)
- Object-C-NSFileManager
- Swift项目兼容Objective-C问题汇总
- object-c(oc)内存管理机制详解
- 使用storyboard设置按钮边框属性(颜色,宽度,圆角)
- Objective-C
- Object-C-复制
- [C++ Object Model] 继承对于member布局的影响
- Objective-C学习笔记---构造函数和析构函数
- C# JArray与JObject 的使用
- Windows环境下编译objective-c
- ETL_inforamtica Objectexport和Objectimport
- (R-CNN)Rich feature hierarchies for accurate object detection and semantic segmentation
- Objective-C中NSLog输出格式大全
- Holding Your Objects 1-4
- Objective-C 反射
- Object-C-属性参数