Phonegap iOS Iframe访问

时间:2013-07-19 11:45:26

标签: ios html5 iframe cordova same-origin-policy

从PhoneGap

访问iOS中的iframe

我很难获得PhoneGap(2.9.0)来访问iOS(6.0.1)上的iframe。

应用程序的index.html包含一个iframe和一个函数,用于检查在加载后是否可以读取帧的内容。

应用程序index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="user-scalable=no, initial-scale=1, 
     maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, 
     target-densitydpi=device-dpi" />
    <script type="text/javascript" charset="utf-8" src="./phonegap.js"></script>
    <title>Test</title>
  </head>
  <body style="background-color: #000;">
    <iframe name="framewrap" id="framewrap"
        style="border:0;position: absolute; top: 0; 
        left: 0;width: 100%; height:100%; background-color: #fff;"
        src="http://www.somedomain.de/somepath/index.html">
    </iframe>

    <script type="text/javascript">
      window.setTimeout(go, 2000);
      function go(){
      iframe = document.getElementById('framewrap');
      innerDoc = iframe.contentWindow;
      alert(innerDoc.document.getElementById('canvas'));
      }
    </script>
  </body>
</html>

iframe中的文档:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <script type="text/javascript">
      alert('iframe loaded');
    </script>
    <title>test</title>
  </head>
  <body style="margin:0;">
  <canvas id="canvas" style="position: absolute; top:0; left:0;">
    <p>Your browser does not support the canvas element.</p>
  </canvas>
  </body>
</html>

会发生什么:

如果我构建应用程序并在Android(4.2.2)上运行它,框架已加载,一个警报告诉我iframe loaded并弹出另一个警告对话框,告诉我有一个HTMLCanvasElement。 但是,如果我在iPod Touch上打开应用程序,我只会收到一个警告对话框,告诉我"null"。 缺少指示iframe的iframe loaded警报根本没有加载。

我已经尝试了很多东西来使这个工作但失败了,因为我没有Mac,我无法正确调试。 -thx apple -

我怀疑:

与app捆绑在一起的html文件作为文件执行,并使用file协议。 iframe使用http协议。因此,相同的原始政策开始,我无法访问iframe。

为什么我不确定:

但这意味着跨平台的行为不一致,因为它在Android上运行。所以我并不完全相信我的怀疑是正确的。 此外,缺少iframe loaded警报会暗示同源策略不是问题,因为至少帧应该像在桌面上那样加载,而同源策略只会阻止画布警报到秀。

因此:

  1. 我的怀疑是否正确?

  2. 有没有办法让它跨平台工作?

  3. 我感谢任何建议,希望有人能帮助我。

    感谢您花时间阅读低谷!

1 个答案:

答案 0 :(得分:0)

我不是iframe的专家,但我会给你一个提示..使用jquery out的加载函数而不是iframe(http://api.jquery.com/load/)或GET你可以返回html形状的返回数据并显示在div ...

也许这是你尝试登上iframe的一个更简单的替代方法......