如何使用Javascript与Objective-c代码进行通信?
在本机 iPhone 应用程序中,我使用 UIWebView
加载网页。
我想在网页中使用 JavaScript 与 Objective-C 代码进行通信,例如调用 Objective-C 代码中的函数。
有没有办法实现这个?
In a native iPhone app, I use a UIWebView
to load a web page.
I want to use JavaScript in the web page to communicate back with the Objective-C code, for example invoke a function in Objective-C code.
Is there any way to implement this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有“直接”的方法来做到这一点,但是您可以利用
UIWebViewDelegate
并捕获具有自定义方案的链接,因此在您的标记中,您可以编写一些内容像这样:然后,在您的
UIWebViewDelegate
的-webView:shouldStartLoadWithRequest:navigationType:
方法中,编写如下内容:There is no "direct" way to do this, but you could utilize the
UIWebViewDelegate
and trap a link with a custom scheme, so in your markup, you'd write something like this:Then, in your
UIWebViewDelegate
's-webView:shouldStartLoadWithRequest:navigationType:
method, write something like this: