简体   繁体   English

使用Ajax从UIWebView调用Objective C函数

[英]Calling Objective C function from UIWebView using Ajax

I found online a tutorial where it says that ObjectiveC code can be called from a WebView using the following Javascript code 我在网上找到了一个教程,其中说可以使用以下Javascript代码从WebView调用ObjectiveC代码

var iframe = document.createElement("IFRAME");
iframe.setAttribute("src", "js-frame:myObjectiveCFunction");
document.documentElement.appendChild(iframe);
iframe.parentNode.removeChild(iframe);
iframe = null;

Then from ObjectiveC I must implement UIWebViewDelegate to receive the shouldStartLoadWithRequest. 然后从ObjectiveC,我必须实现UIWebViewDelegate来接收shouldStartLoadWithRequest。 I tried this code and it worked; 我尝试了这段代码,它起作用了; however, I was wondering if the same functionality could be implemented using an AJAX call instead of using an IFrame. 但是,我想知道是否可以使用AJAX调用而不是IFrame来实现相同的功能。

It can be done if you change window.location with JS. 如果您使用JS更改window.location,则可以完成此操作。 it will invoke the delegate function you're using. 它将调用您正在使用的委托函数。

You may also use links on your page to execute an obj-c function using the same delegate method. 您也可以使用同一委托方法,使用页面上的链接来执行obj-c函数。

If you're planning to do a special call using AJAX, please explain it more. 如果您打算使用AJAX拨打特殊电话,请进一步说明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM