Search code examples
javascriptiossettingssettings.bundle

Injecting javascript with data from settings app


I have setup a settings bundle and can successfully pull data from that. I also need to inject some code into a UIWebView which I can also successfully do. Now the question is How do I combine the 2?

I have this code to read a textField label from the setting bundle:

NSString *textField01 = [[NSUserDefaults standardUserDefaults] stringForKey:@"tetxtField01_preference"];

and i have this string to inject code into the UIWebView:

 [_webView stringByEvaluatingJavaScriptFromString:@"document.getElementById('textField01').value = 'default text'"];

Now in the stringByEvaluatingJavaScriptFromString I can inject the "default text" but then it is hardcoded into the app and I cannot change it within the app.

So is it possible and if so how do I put objective-c NSString in the place default text?

I was thinking of something along the lines of:

[_webView stringByEvaluatingJavaScriptFromString:@"document.getElementById('textField01').value = [@"%@", textField01] "];

but it gives an error:-)

Cheers


Solution

  • Create the Js string completely and pass it on to the "stringByEvaluatingJavaScriptFromString:" function. i.e

    NSString *foo = [NSString stringWithFormat:@"document.getElementById('textField01').value = %@",textField01]; [_webView stringByEvaluatingJavaScriptFromString:foo];