【问题标题】:Render HTML5 chart in WebView using Javascript使用 Javascript 在 WebView 中渲染 HTML5 图表
【发布时间】:2012-02-27 11:23:57
【问题描述】:

我正在尝试在我的 MacOS 应用程序中构建图表,并决定与其使用本机 Objective-c 中可用的有限选项,不如使用 HTML 5 并嵌入 WebView 控件。

我已经成功地嵌入了 WebView,方法是使用以下链接到我的应用程序中的 index.html 文件。

// Load the HTML content.
NSString *resourcesPath = [[NSBundle mainBundle] resourcePath];
NSString *htmlPath = [resourcesPath stringByAppendingString:@"/index.html"];
[[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:htmlPath]]];

我用一些简单的 html 对此进行了测试,所以我知道它连接正常。但是,当嵌入一些更复杂的代码(包括一些 CSS 和 JS)时,它不会呈现。如果我在 Safari 窗口中加载 index.html,它会呈现图表。

要让 Javascript 执行,我还需要做些什么吗?

更多细节,图表使用JQPlot,索引文件的内容是……

<html>
<head>
  <script language="javascript" type="text/javascript" src="jquery.min.js"></script>
  <script language="javascript" type="text/javascript" src="jquery.jqplot.min.js"></script>
  <link rel="stylesheet" type="text/css" href="jquery.jqplot.min.css" />
</head>

<body>

  <div id="chartdiv" style="height:200px;width:300px; "></div>

  <script>
     $.jqplot('chartdiv',  [[[1, 2],[3,5.12],[5,13.1],[7,33.6],[9,85.9],[11,219.9]]]);
  </script>    
</body>
</html>

更新:

我进行了更多的分析,我发现我可以执行标准的 javascript,但是 jQuery 不工作。所以,我猜这个问题与在 html 页面顶部加载脚本标签有关。

【问题讨论】:

    标签: macos webkit


    【解决方案1】:

    事实证明,JS 文件正在被编译,而不是简单地被复制。我在这里发现了一个类似的问题 - iPhone UIWebView local resources using Javascript and handling onorientationChange

    答案是在点击主目标后转到 Build Phases 选项卡,并将 JS 文件从 Compile Sources 列表移动到 Copy Bundle Resources 列表。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-12
      • 1970-01-01
      • 2013-10-21
      • 1970-01-01
      相关资源
      最近更新 更多