对于 android 有一个名为 addJavascriptInterface()
的方法,它可以将 Android JAVA 对象导入到 Javascript 上下文中。 iOS 上是否有任何等效项?
我已经解决了一些类似的问题:
androids-addjavascriptinterface-equivalent-in-ios
calling-objective-c-function-from-javascript-in-ios-applications
但找不到适合我的问题的解决方案。
function initiateAddCall(){
var locus = reader.getPlace().getLocus();
RoomHelper.postReaderLocus(JSON.stringify(locus));
}
RoomHelper
是使用 .有什么方法可以在 UIWebView
iOS 中实现这个(在 UIWebView iOS 中注册一个 javascript 处理程序/接口(interface))。
最佳答案
我刚刚使用了 EasyJSWebView并且工作得很好。 还有其他解决方案,例如 WebViewJavascriptBridge但 EasyJSWebView 更易于使用。
RoomHelper *roomHelper = [RoomHelper new];
[self.myWebView addJavascriptInterfaces:interface WithName:@"RoomHelper"];
[self.myWebView injectJS:webView];
在你调用的javascript中:
RoomHelper.test()
然后在 RoomHelper 中实现一个名为 test 的方法
房间助手.h :
@interface RoomHelper : NSObject
- (void) test;
@end
关于javascript - iOS 上的 UIWebView 是否有向 DOM 添加接口(interface)的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33318008/