我需要用存储在目标 C 中的变量填充 html webview。
我相信我需要使用:
[self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"John", firstName];
但不确定要放什么以及我的javascript需要什么来实现传递的变量。
这是我的 html 的一点点,可以帮助您了解一下:
<ul><li><span>First Name:</span> first name needs to go here</li>
<li><span>Last Name:</span> last name needs to go here</li>
更新代码:
- (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];
}
更新 2: javascript/html:
<ul><li><span>Policy Number:</span>
<script> function myFunc(str)
{
document.write(str) }
</script>