Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a simple javascript function that takes two variables. I need to pass two variables that I already have in my Objective-C (iOS) application to this javascript function. My line of code to run the javascript is:

[webView stringByEvaluatingJavaScriptFromString:@"onScan()"];

The javascript function just applies the two variables to a HTML form and submits it. I'm of course getting undefined in my form since the variables are missing. 8-) I've been unable to find much documentation on this, but maybe I'm looking in the wrong places?

FWIW, my Objective-C variables are strings. My javascript function is onscan(a,b)

UPDATE:

I was able to get this working by placing single quotes around each of the variables being passed to the javascript function. The updated code is:

[webView stringByEvaluatingJavaScriptFromString:@"onScan('%@','%@')",a,b];
share|improve this question

1 Answer 1

up vote 4 down vote accepted

stringByEvaluatingJavaScriptFromString: takes an NSString so all you have to do is combine strings using stringWithFormat: and the %@ object formatter like below:

NSString *stringOne = @"first_parameter";
NSString *stringTwo = @"second_parameter";

NSString *javascriptString = [NSString stringWithFormat:@"onScan('%@','%@')", stringOne, stringTwo];

[webView stringByEvaluatingJavaScriptFromString:javascriptString];

Check out Apple's documentation for NSString, it's an insanely useful object!

share|improve this answer
    
Thanks, I had already tried that and I couldn't get anything to pass to my UIWebView. I put the code back (like you had above) and placed the string into NSLog to make sure things were kosher, but for some reason the javascript function isn't using those variables being passed to it using this method. Weird that I get an undefined in the form text value box when I don't place any variables in the string. –  Donavon Yelton Feb 1 '12 at 18:17
1  
I was able to resolve this by wrapping the variables to pass to the function in single quotes. I have updated my question with the answer. –  Donavon Yelton Feb 1 '12 at 18:26
    
Whoops thanks for catching that, I've updated my answer as well. I kept being bugged by this feeling that I was forgetting something... –  Jack Lawrence Feb 1 '12 at 18:29
    
@DonavonYelton Post it as an answer and accept it then. –  jrtc27 Feb 1 '12 at 18:31
    
@jrtc27 I can't answer my own question as my reputation is below 100 and not enough time has passed, however I have awarded the answer to Jack Lawrence even though I had already tried his first reply. The missing part was the single quotes wrapped around the variables. –  Donavon Yelton Feb 1 '12 at 18:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.