【问题标题】:Calling Javascript Function With Variables from Native iOS App使用原生 iOS 应用程序中的变量调用 Javascript 函数
【发布时间】: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


    【解决方案1】:

    我找不到调用在我加载到 UIWebView 的 html 文档中定义的 javascript 函数的方法(我不知道这是否可行)。但是我发现如果我不使用单独的 html 文件,而是直接在我的目标 c 代码中创建的 html 字符串,我可以实现我的目标。我能够将变量传递给创建 html 字符串的函数,并且我也能够在 html 字符串中运行我的 javascript。

    这是我创建 html 字符串的方法:

    - (NSString *)htmlString:(NSString *)emailMessage {
    
        NSString *imageName = @"fauxImageAttachment2.JPG";
    
        NSString *string = [NSString stringWithFormat:@""
        "<!doctype html>"
        "<head>"
        "<meta charset='utf-8'>"
        "<meta name='viewport' content='width=device-width,initial-scale=1'>"
        "<link rel='stylesheet' href='inlineAttachmentTemplateStyling.css'>"
        "</head>"
    
        "<body id='body'>"
        "<div id='container'>"
        "<script type='text/javascript'>"
        "document.write('%@');"
        "</script>"
        "<img id='testImage' src='%@' />"
        "</div><!-- #container -->"
        "</body>"
    
        "</html>", emailMessage, imageName];
    
      return string;
    }
    

    这是我将它加载到 webview 中的方式:

    - (EmailView *)loadHTMLIntoEmailView:(EmailView *)emailView
    {
       NSString *sampleEmailMessage = @"Hey! Check out this awesome photo I sent you!!!!!";
       NSString *path = [[NSBundle mainBundle] bundlePath];
       NSURL *baseURL = [NSURL fileURLWithPath:path];
        [emailView.webView loadHTMLString:[self htmlString:sampleEmailMessage] baseURL:baseURL];
    
        return emailView;
    }
    

    不确定这是否是最好的方法,但它适用于我的目的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多