objectc 与 js 交互
来源:互联网 发布:小腿肌肉 知乎 编辑:程序博客网 时间:2024/06/06 13:20
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
#import <JavaScriptCore/JavaScriptCore.h>
List of Main Classes supported by framework:
JSContext : For any javascript operations you need JSContext.
JSContext
*context = [[
JSContext
alloc
]
init
];
JSValue : JSValue holds JavaScript values,variables and functions. List of main methods in JSValue.
// Convert this value to a corresponding Objective-C object, according to the
// conversion specified above.
- (
id
)toObject;
// Convert this value to a corresponding Objective-C object, if the result is
// not of the specified class then nil will be returned.
- (
id
)toObjectOfClass:(Class)expectedClass;
// The value is copied to a boolean according to the conversion specified by the
// JavaScript language.
- (
BOOL
)toBool;
// The value is copied to a number according to the conversion specified by the
// JavaScript language.
- (
double
)toDouble;
// The value is copied to an integer according to the conversion specified by
// the JavaScript language.
- (int
3
2
_t)toInt
3
2
;
// The value is copied to an integer according to the conversion specified by
// the JavaScript language.
- (uint
3
2
_t)toUInt
3
2
;
// If the value is a boolean, a NSNumber value of @YES or @NO will be returned.
// For all other types the value will be copied to a number according to the
// conversion specified by the JavaScript language.
- (
NSNumber
*)toNumber;
// The value is copied to a string according to the conversion specified by the
// JavaScript language.
- (
NSString
*)toString;
// The value is converted to a number representing a time interval since 1970,
// and a new NSDate instance is returned.
- (
NSDate
*)toDate;
// If the value is null or undefined then nil is returned.
// If the value is not an object then a JavaScript TypeError will be thrown.
// The property "length" is read from the object, converted to an unsigned
// integer, and an NSArray of this size is allocated. Properties corresponding
// to indicies within the array bounds will be copied to the array, with
// Objective-C objects converted to equivalent JSValues as specified.
- (
NSArray
*)toArray;
// If the value is null or undefined then nil is returned.
// If the value is not an object then a JavaScript TypeError will be thrown.
// All enumerable properties of the object are copied to the dictionary, with
// Objective-C objects converted to equivalent JSValues as specified.
- (
NSDictionary
*)toDictionary;
Accessing Javascript Code in Objective-C
1).Evaluating Javascript Code
NSString
* jsCode =
@"1+2"
;
JSContext
*context = [[
JSContext
alloc
]
init
];
JSValue
* value = [context
evaluateScript
:jsCode];
NSLog(
@"Output = %d"
, [value
toInt32
]);
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"]
JSContext
*context = [[
JSContext
alloc
]
init
];
NSString
* jsCode =
@"var x; x=10;"
;
[context
evaluateScript
:jsCode];
JSValue
* a =context[
@"x"
];
NSLog(
@"x = %d"
, [a
toInt32
]);
3).Access Javascript functions and execute them in Objective-C
//function with arguments
JSContext
*context = [[
JSContext
alloc
]
init
];
NSString
* jsCode =
@"function sum(a,b) { return a+b;} "
;
[context
evaluateScript
:jsCode];
JSValue
* func =context[
@"sum"
];
NSArray
* args = @[[
NSNumber
numberWithInt
:
1
0
],[
NSNumber
numberWithInt
:
2
0
]];
JSValue
* ret =[func
callWithArguments
:args];
NSLog(
@"10+20 = %d"
, [ret
toInt32
]);
//function without arguments
NSString
* jsCode
2
=
@"function getRandom() { return parseInt(Math.floor((Math.random()*100)+1));} "
;
[context
evaluateScript
:jsCode
2
];
JSValue
* func
2
=context[
@"getRandom"
];
for
(
int
i=
0
;i<
5
;i++)
{
JSValue
* ret
2
=[func
2
callWithArguments
:nil
];
NSLog(
@"Random Value = %d"
, [ret
2
toInt32
]);
}
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.
JSContext
*context = [[
JSContext
alloc
]
init
];
//we are telling that "sum" is function, takes two arguments
context[
@"sum"
] = ^(
int
arg
1
,
int
arg
2
)
{
return
arg
1
+arg
2
;
};
NSString
* jsCode =
@"sum(4,5);"
;
JSValue
* sumVal = [context
evaluateScript
:jsCode];
NSLog(
@"Sum(4,5) = %d"
, [sumVal
toInt32
]);
//we are telling that "getRandom" is function, does not take any arguments
context[
@"getRandom"
] = ^()
{
return
rand()%
1
0
0
;
};
NSString
* jsCode
2
=
@"getRandom();"
;
for
(
int
i=
0
;i<
5
;i++)
{
JSValue
* sumVal
2
= [context
evaluateScript
:jsCode
2
];
NSLog(
@"Random Number = %d"
, [sumVal
2
toInt32
]);
}
2.USING JSEXPORT PROTOCOL
You can see the comparison between Objective-C Object and it’s equivalent Javascript Object below.
@interface
MyObject
@property
int
x;
-(
int
) getX;
+(
MyObject
*)
getSqure
:(MyObject*)obj;
@end
//Usage:
MyObject
* obj = [
MyObject
new
];
[obj
getX
];
[
MyObject
getSqure
:obj];
function
MyObject()
{
//member variable
this
.x=0;
//instance method
this
.getX =
function
()
{
return
this
.x;
}
}
//static method
MyObject.getSqure =
function
(obj)
{
var
newObj =
new
MyObject();
newObj.x = obj.x * obj.x;
return
newObj;
}
//Usage:
var
obj =
new
MyObject();
obj.x=10;
var
sqrObj = MyObject.getSqure(obj);
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.
@protocol
MyObjectExport
@property
int
x;
-(
int
) getX;
+(
MyObject
*)
getSqure
:(MyObject*)obj;
@end
@interface
MyObject
:
NSObject
<MyObjectExport>
//this method is not accessible in javascript
-(
void
) test;
@end
;
@implementation
MyObject
@synthesize
x;
-(
int
) getX
{
return
self
.x
;
}
+(
MyObject
*)
getSqure
:(MyObject*)obj;
{
NSLog(
@"Calling getSqure"
);
MyObject
* newObj = [
MyObject
new
];
newObj
.x
= obj
.x
* obj
.x
;
return
newObj;
}
Now MyObject(Objective-C)’s variables and functions are directly accessed from javascript.
JSContext
* context = [[
JSContext
alloc
]
init
];
NSString
* jsCode =
@"function sqrtOf(obj){ return MyObject.getSqure(obj);}"
;
//adding Objective-C Class to Javascript.
context[
@"MyObject"
]=[
MyObject
class
];
[context
evaluateScript
:jsCode];
MyObject
* obj =[
MyObject
new
];
obj
.x
=
1
0
;
JSValue
* func =context[
@"sqrtOf"
];
JSValue
* val = [func
callWithArguments
:@[obj]];
//we got MyObject from javascript.
MyObject
*sqrtObj = [val
toObject
];
NSLog(
@"Value =%d, %d"
, obj
.x
, [sqrtObj
getX
]);
- objectc 与 js 交互
- Flash与JS交互
- AS3与JS交互
- ActiveX与JS交互
- siverlight与js交互
- js与c#交互
- flex与js交互
- ActiveX与JS交互
- iphone 与 js 交互
- CDHtmlDialog与js交互
- webview与js交互
- applet与js交互
- webview与js交互
- AS 与 JS 交互
- uiwebview与js交互
- webview与js交互
- Silverlight与JS交互
- UIWebView与js交互
- Drill for windows 安装以及使用
- MFC文本编程概述
- NYOJ927The partial sum problem(暴力dfs水过)
- Android的onCreateOptionsMenu()创建菜单Menu详解
- 使用Genymotion调试出现错误INSTALL_FAILED_CPU_ABI_INCOMPATI
- objectc 与 js 交互
- BAT时代
- flume学习(十一):如何使用Spooling Directory Source
- Tiny实例:TINY框架官网制作过程详解
- BZOJ4104【乱搞】
- hdu5247 找连续数
- 使用SharedPreferences存取数据
- 使用TinySpider实战抓取自己博客中的内容
- MyBatis3整合Spring3、SpringMVC3