I am migrating my app from android to iOS and for following line, compiler returns me an error on iOS,
webviewA.loadUrl("javascript:setVars(\"" + var1 + "\",\"" + var2 + "\")"); //properly executed on Android
for iOS I am trying,
[webViewA stringByEvaluatingJavaScriptFromString:@"javascript:setVars(\"" + var1 + "\",\"" + var2 + "\")"];
which would be correct syntax for iOS? Thank you.
As per this answer on another similar question, you need to implement the
UIWebViewdelegate methodwebViewDidFinishLoad:, and in there implement[myWebView stringByEvaluatingJavaScriptFromString:@"myJavaScriptCode()"].In order for this to work you need to set the delegate on the
UIWebViewinstance and implement the delegate method mentioned above so that the JavaScript is executed after the page has loaded.The documentation for
- (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)scriptcan be found here.Now for the other part of your question (thanks for clarifying!).. to concatenate strings in Objective-C you can use the
NSStringclass methodstringWithFormat:.For example, to pass two Objective-C values to a JavaScript string, you could do something like this: