【发布时间】:2013-08-29 14:07:04
【问题描述】:
我目前有一个成功加载 html 文件的 web 视图。我的方法看起来像:
- (EmailView *)loadHTMLIntoEmailView:(EmailView *)emailView
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"inlineAttachmentTemplate" ofType:@"html"];
NSData *htmlData = [NSData dataWithContentsOfFile:path];
NSString *resourceURL = [[NSBundle mainBundle] resourcePath];
resourceURL = [resourceURL stringByReplacingOccurrencesOfString:@"/" withString:@"//"];
resourceURL = [resourceURL stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
NSURL *baseURL = [NSURL URLWithString:[NSString stringWithFormat:@"file:/%@//",resourceURL]];
[emailView.webView loadData:htmlData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:baseURL];
return emailView;
}
我拥有的 html 文件如下所示:
<!doctype html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<link rel="stylesheet" href="inlineAttachmentTemplateStyling.css">
<script>
function myFunction()
{
document.write("Hello from my html file!!!!");
}
</script>
</head>
<body id="body">
<div id="container">
<img id="testImage" src="fauxImageAttachment2.JPG" />
</div><!-- #container -->
</body>
</html>
这是因为图像出现在 web 视图中。如果我将 document.write 从函数中取出并将其直接放在脚本标签中,则文本会出现在 webview 中。但是,我需要能够调用允许文本显示在 webview 中的函数(从我的 Objective C 方法),最终我还需要向该函数传递一些变量。目前,我什至无法弄清楚如何调用 javascript 函数。
我查看了这个示例,Can you call a javascript function from native code (not in a callback) using PhoneGap and iOS?,并尝试在我的目标 C 方法中添加以下行:
[emailView.webView stringByEvaluatingJavaScriptFromString:@"myFunction()"];
但这并没有调用函数……或者至少文本没有出现在 webview 中。
我是一个新手程序员,对 javascript 知之甚少 - 谁能指出我做错了什么?
【问题讨论】:
标签: javascript html ios objective-c uiwebview