Communication between JavaScript and OBJECTIVE-C

Source: Internet
Author: User

Before their idle bored to write a few application of the Web page "purse software", in fact, like this "purse software" can also do very high-end atmosphere, through the WebKit implementation of Oc-js Bridge, can be very convenient to implement in JS call OC/in OC Call JS method. In this way, you can implement the flashy UI through the web, and natively-related operations through the native API.

1, JS in the type of variable in OC
With Oc-js Bridge, the type of the variable is automatically converted, and the basic type is automatically converted, such as the number in JS, the Boolean is converted to the NSNumber type in OC, and the string type is automatically converted to the NSString type. The object in JS is converted to Webscriptobject object, and the related property information can be read and written by Key-value method, and the related code can be seen later in this article.

2, implementation of the JS method called in OC
It is very convenient to invoke the JS method in OC, WebView has a Windowscriptobject property that allows you to get the script object directly, and then you can call Callwebscriptmethod: Witharguments the message to the method and parameters of the object in JS, for a simple method call you can also directly through the WebView method stringbyevaluatingjavascriptfromstring to execute a section of JS code, and returns a string. Example code:

 1 
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 //call in OC 
-(void) Ocaction: (ID) sender
{
Nsarray *args = @[@ "hello,js!"];
ID result = [[WebView windowscriptobject] callwebscriptmethod:@ "Jsfunction" witharguments: Args];
NSLog (@ "%@", result);
//the corresponding method in JS
function jsfunction (parameter)
{
//display the value returned by OC
alert (parameter);
}
/span>

3, the realization of JS call OC method
By setting WebView's Frameloaddelegate, in the –webview:didclearwindowobject:forframe: Callback method, specify a local object (the object implements the Webscripting protocol). Then JS can directly invoke the object's related methods.
The code in OC is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
This method is used in JS to invoke
-(Webscriptobject *) Status: (Webscriptobject *) jsobject
{
To display the information that JS sent over
NSString *message = [Jsobject valueforkey:@ "message"];
NSLog (@ "%@", message);

Returns the successful information (the Webscriptobject object cannot be created by itself, so the parameters passed in here are reused)
[Jsobject SetValue:@ "The local side has received the message!" "Forkey:@ "message"];
return jsobject;
}

#pragma mark-
#pragma Mark Webframeloaddelegate

Through this callback, the self is passed to the JS environment
- (void) WebView: (WebView *) sender Didclearwindowobject: (Webscriptobject *) WindowObject forframe: (Webframe *) frame
{
[WindowObject SetValue:Self Forkey:@ "native"];
}

#pragma mark-
#pragma Mark Webscriptingprotocol

/*
Returns whether the method is blocked from responding,
Return no to respond to this method
*/
+ (BOOL) Isselectorexcludedfromwebscript: (SEL) Selector
{
if (selector = =@selector (Status:))
{
return no;
return yes;
/*
Returns the name of the local method in JS (this method is not implemented, the method name in JS is the same as in OC)
*/
+ ( {
if (sel = = @selector (status:))
{
return @ "Ocmethod";
return nil;

/span>

The code in JS is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
 function callnative () 
{
if (native)
//the message is assembled into an object to OC
Span class= "line" > var parameter = { var result = Native.ocmethod (parameter);

//show results of OC return
alert (Result [ /span>

The relevant sample code can be found in Apple's Calljs demo, or you can download the Demo:OCJS_Bridge.zip I wrote directly.

Communication between JavaScript and OBJECTIVE-C

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.