[英]Does UIWebView on iOS have methods to add interface to DOM?
對於android,有一個名為addJavascriptInterface()
的方法, addJavascriptInterface()
Android JAVA對象導入Javascript上下文。 在iOS上有相同的嗎? 我已經經歷了一些類似的問題:
機器人-addjavascriptinterface等效功能於IOS
調用-Objective-C的功能,從JavaScript的功能於IOS的應用程序
但無法找到適合我問題的解決方案。
function initiateAddCall(){
var locus = reader.getPlace().getLocus();
RoomHelper.postReaderLocus(JSON.stringify(locus));
}
RoomHelper
是我正在使用的javascript處理程序。 有沒有什么辦法可以實現這個( 注冊在UIWebView的iOS的一個JavaScript處理/接口 )在UIWebView
的iOS。
我剛剛使用EasyJSWebView並且工作得非常好。 還有其他解決方案,如WebViewJavascriptBridge,但EasyJSWebView更易於使用。
RoomHelper *roomHelper = [RoomHelper new];
[self.myWebView addJavascriptInterfaces:interface WithName:@"RoomHelper"];
[self.myWebView injectJS:webView];
在javascript中你打電話:
RoomHelper.test()
在RoomHelper中,您實現了一個名為test的方法
RoomHelper.h:
@interface RoomHelper : NSObject
- (void) test;
@end
不得不將它用於本機應用程序項目,我使用D3.JS來顯示圖形。 使用下面的代碼來調用JS函數
NSString *jsString = [NSString stringWithFormat:@"showLegendGraphsFromDevice('%@',%d)",sectionName, position];
[_webView stringByEvaluatingJavaScriptFromString:jsString];
這里
function showLegendGraphsFromDevice(name, position)
是JS函數
這里的關鍵是使用所需的引號創建正確的JS字符串。
您可以使用JavascriptCore來實現addJavascriptInterface
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.