UIWebView 中的 Javascript 回调到 C/Objective-C [英] Javascript in UIWebView callback to C/Objective-C
问题描述
当在 UIWebView 中检测到某个事件时,有没有办法让 Objective-c 回调?Javascript 可以向 Objective-C 发送回调吗?
Is there a way to get a callback to objective-c when a certain event has been detected in a UIWebView? Can Javascript send a callback to Objective-C?
推荐答案
更新 - 不再使用 UIWebView.使用 WKWebView,或者更好的(如果它满足您的需求并且您正在为 iOS 9 构建),一个 Safari 视图控制器.
Update - don't use UIWebView anymore. Use WKWebView, or better yet (if it fits your needs and you're building for iOS 9), a Safari View Controller.
但如果您必须使用 UIWebView,请在您的 UIWebView 委托中提供 webView:shouldStartLoadWithRequest:navigationType:
But if you must use UIWebView, in your UIWebView delegate, provide an implementation for webView:shouldStartLoadWithRequest:navigationType:
在您的 HTML 或 Javascript 文件中,添加将 URL 发送到自定义方案的函数(出于可读性目的,不需要自定义方案).发送的所有 URL 都会传递给您的 Objective-C 方法实现,然后您就可以为所欲为.
In your HTML or Javascript files, add functions that send URLs to a custom scheme (for readability purposes, the custom scheme isn't required). All the URLs sent will be passed to your Objective-C method implementation, and then you can do what you'd like.
这篇关于UIWebView 中的 Javascript 回调到 C/Objective-C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!