【发布时间】:2012-11-06 15:58:56
【问题描述】:
我几乎使用了这里列出的这个过程,以允许 javascript 从我的 iPad 应用程序调用目标 c:
http://www.stevesaxon.me/posts/2011/window-external-notify-in-ios-uiwebview/
我注入页面的 javascript 代码是:
<script type="text/javascript">;
window.external =
{
'Set_A': function(s) { document.location = 'qms://Set_A?param=' + s; },
'Get_A': function(s) { document.location = 'qms://Get_A'; },
'Set_B': function(s) { document.location = 'qms://Set_B?param=' + s; },
'Get_B': function(s) { document.location = 'qms://Get_B'; },
'Set_C': function(s) { document.location = 'qms://Set_C?param=' + s; },
'Get_C': function(s) { document.location = 'qms://Get_C'; },
'Get_Version': function(s) { document.location = 'qms://Get_Version'; }
};
</script>;
而html代码中调用上述方法进行初始化的javascript为:
<html>
<body>
...
<script type="text/javascript">
Get_Version();
Set_A(true);
Set_B(false);
Set_C(true);
<script>
</body>
</html>
在上面的 html 中,Objective C 中唯一调用的方法是最后一个方法 Set_C(true);
谢谢。
【问题讨论】:
-
我怀疑您需要将所有调用链接到一个......即只有在 javascript 中设置的最后一个 document.location 会被转发到您的应用程序。
-
你可以尝试像
setTimeout(function() { Set_A(true); }, 1)一样异步调用这些函数。我猜 UIWebView 不会在每一行之后停止执行你的 JS 并加载一个新页面。所以只有最后一个有任何影响。 -
所有的document.location都得到“设置”,也就是说我可以在加载后点击html页面上的链接或按钮并调用objective-c,只要它只是一个函数调用。
-
我不熟悉 setTimeout js 方法。我会复习一下并试一试。在 uiwebviewdelegate 方法 webView shouldStartLoadWithRequest 中,我们在初始页面加载后返回 no(每当 js 代码发送到 Objective-c 时)。我不确定如何更好地捕获这些调用。
标签: javascript ios uiwebviewdelegate