1

Now I want to transform a Array from object_c to javascript, just like label1 , label2 , label3... in the array. I use this method :

WebScriptObject* win = [webview windowScriptObject];
NSMutableArray *nameArray; // in this have some file name : label1,label2...
[win callWebScriptMethod:@"transArray" withArguments:nameArray];

and then in the javascript I var a array and implement a func:

var labelArray= []; // maybe var labelArray = new Array (%@);but it didn't work
function transArray(param)
{
    for(var i=0;i < param.length;i++)
    labelArray[i] = param[i];
}

then I found that the labelArray isn't like I want. it just like : labelArray[0] = l,labelArray[1] = a; labelArray[2] = b..... .I thought maybe I could var labelArray = new Array (%@).but it didn't work. I don't know the javascript clearly , and if I can transform a array from object_c to javascript or not , how to do ? thanks!


Now I found the method that transformming the argu from object_c to Javascript. But I didn't know how to get the array in the javascript , I thought maybe my method was wrong , but how to get the array and var the array,Thanks !

4 Answers 4

1
id win = [webView windowScriptObject];

NSArray *args = [NSArray arrayWithObjects:
                    @"sample_graphic.jpg",
                    [NSNumber numberWithInt:320],
                    [NSNumber numberWithInt:240],
                    nil];

[win callWebScriptMethod:@"addImage"
            withArguments:args];

from WebKit Objective-C Programming Guide

Sign up to request clarification or add additional context in comments.

2 Comments

I know the method from object_c to javascript , but how to get the array argu in the javascript ?
jin: That's what the withArguments: argument is for. It uses the contents of the array as the arguments to the JavaScript function named (in this example) addImage.
0

You could take a peek at JSON encoding/decoding. This is by far the easiest way I know of to get data from and to javascript.

http://www.json.org/ has a list of available classes at the bottom of the homepage. I'm pretty sure there is one that will suit your needs.

Hope this helps!

Comments

0

ok I found the answer: function addImage() { for(var i=0;i

Comments

0

You should code like this:

[win callWebScriptMethod:@"transArray" withArguments:@[nameArray]];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.