Can anyone guide me on how to populate an HTML webview with variables stored in Objective-C?
I assume I need to use the following code snippet:
[self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"John", firstName];
However, I am unsure about what exactly should be placed there and what my JavaScript code needs to be in order to correctly implement the passed variables.
Here is a snippet of the HTML code for context:
<ul><li><span>First Name:</span> first name goes here </li>
<li><span>Last Name:</span> last name goes here</li>
Updated code:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"idcard" ofType:@"html"]isDirectory:NO]]];
}
-(void)viewDidAppear:(BOOL)animated
{
NSString *str = @"John";
NSString *script = [NSString stringWithFormat:@"myFunc('%@')", str];
[self.webView stringByEvaluatingJavaScriptFromString:script];
}
Update 2: javascript/html:
<ul><li><span>Policy Number:</span>
<script> function myFunc(str)
{
document.write(str) }
</script>