【问题标题】:Calling UIWebViewDelegate method "when I want"“当我想要”时调用 UIWebViewDelegate 方法
【发布时间】:2012-05-04 23:51:36
【问题描述】:

我正在使用webView:shouldStartLoadWithRequest:navigationType 方法从JavaScript 调用我的Objective-C 代码,它工作正常。问题是,这个方法只有在 Web 视图重新加载时才会被调用。我希望能够“随时随地”发送消息。这可能吗?

具体来说,我想尽可能频繁地使用document.location = "myapp:" + "customerAdded:" + "YES";。我的代码只有在我使用 Google API V3 绘制地图时才会执行。

是否可以重新加载“不可见”框架以调用委托方法?

【问题讨论】:

    标签: javascript iphone cocoa-touch uiwebview


    【解决方案1】:

    试试这个,对我来说很好用

    function invokeObjectiveC() {
        var iframe = document.createElement("IFRAME");
        iframe.setAttribute(your stuff here);
        document.documentElement.appendChild(iframe);
        iframe.parentNode.removeChild(iframe);
        iframe = null;
    }
    

    【讨论】:

    • 所以这个方法会触发webView:shouldStartLoadWithRequest:navigationType ?
    【解决方案2】:

    webView:shouldStartLoadWithRequest:navigationType: 应该只在 web 视图重新加载时调用,这就是这个委托的目的。您可以使用NSNotificationCenter 调用重新加载您的webview 的方法。

    【讨论】:

    • 是否必须重新加载整个页面才能调用代理?
    • 是的,您仍然无法加载网页的一部分
    猜你喜欢
    • 2015-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-29
    • 1970-01-01
    相关资源
    最近更新 更多