开发者

How to display the contents of the array in Javascript.The array is present in ObjectiveC Code

开发者 https://www.devze.com 2023-04-03 04:40 出处:网络
How to display the contents of the array in Javascript.The array is present in ObjectiveC Code, The following is the Objective C Code,

How to display the contents of the array in Javascript.The array is present in ObjectiveC Code,

The following is the Objective C Code,

[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"Array"ofType:@"html"]isDirectory:NO]]];

        NSString *path = [[NSBundle mainBundle] pathForResource:@"Example" ofType:@"csv"];
        NSString *contents = [NSString stringWithContentsOfFile:path encoding:NSASCIIStringEncoding error:nil];
        NSArray *lines = [contents componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"\n,"]];
        for (NSString* line in lines) {

                 NSLog(@"%@", line);


                }
                NSLog(@" %d", 开发者_如何学编程[lines count]);

So how to link this .m file to the Html or Javascript, so that i can display the contents of array on OnClick of Button. This Question may be simple. But i am new to javascript and Html, so i dont know what tags or code to use to link the .m file and Html file.


If you don't know HTML and JavaScript, it's really beyond this question, you have to learn both and then get back to Cocoa.

Once you know both (and you have learned dom manipulation in JavaScript) you could use something like this to do what you want:

function showArrayContents( array ) {
  // do something in JavaScript here to show the array
}

And on Objective-C, you can call this function like this:

[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"Array"ofType:@"html"]isDirectory:NO]]];

NSString *path = [[NSBundle mainBundle] pathForResource:@"Example" ofType:@"csv"];
NSString *contents = [NSString stringWithContentsOfFile:path encoding:NSASCIIStringEncoding error:nil];
NSArray *lines = [contents componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"\n,"]];    

// this is where you call the JS function above

WebScriptObject * scriptObject = [ webView windowScriptObject ];
[scriptObject callWebScriptMethod: @"showArrayContents" withArguments: [NSArray arrayWithObject: withArguments ]]
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号