3

How to pass a ObjectiveC string Variable to a javascript function? I have just started with javascript.. so plz 4give my ignorance...

Felipe Sabino
  • 17,825
  • 6
  • 78
  • 112
djk
  • 53
  • 2
  • 6
  • Some more information would be useful. Maybe a code snippet? – Aatch Sep 04 '10 at 12:04
  • Suppose the JS function is myFunc(var arg1) I Have NSString * str = some value. Now, [webView stringByEvaluatingJavaScriptFromString:.......] Here how to pass the str as a argument to the Js function myFunc() – djk Sep 04 '10 at 12:32

2 Answers2

12
NSString * param  = @"foo";   
NSString * jsCallBack = [NSString stringWithFormat:@"myFunc('%@')",param];
[webView stringByEvaluatingJavaScriptFromString:jsCallBack];
Aaron Saunders
  • 33,180
  • 5
  • 60
  • 80
4

In Objective-C

NSString *str  = @"hi ya";   
[self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"myFunc('%@')",str]];

Now call this function in the js file:

function myFunc(str)
{
   alert(str);
}

These steps should work.

Kara
  • 6,115
  • 16
  • 50
  • 57
Saurabh
  • 331
  • 4
  • 12