3

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

2
  • Some more information would be useful. Maybe a code snippet? Commented Sep 4, 2010 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() Commented Sep 4, 2010 at 12:32

2 Answers 2

12
NSString * param  = @"foo";   
NSString * jsCallBack = [NSString stringWithFormat:@"myFunc('%@')",param];
[webView stringByEvaluatingJavaScriptFromString:jsCallBack];
Sign up to request clarification or add additional context in comments.

2 Comments

What if my param is object or array? Not a "foo" string?
Is there any alternate way. I am trying to pass my complete web-service response so it is crashing.
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.