Because recent projects need cross-platform, find this demo on the Web to use. First of all, the cross-platform: Because many products now have an Android version with the iOS version, it means that the same thing to two sets, by two groups of people to complete, not only increased the development costs, but also greatly exacerbated the maintenance costs. Smart coder came up with a cross-platform approach, using HTML to write pages, respectively, with WebView (iOS), (Android) to load, to some HTML can not be called hardware, through mutual interaction to achieve the method of mutual harmonic transfer value. This process is cross-platform.
Here's how Webviewjavascriptbridge is used on the iOS side. First, make sure that you have an HTML file that is well-equipped. (HTML is still in the study stage, temporarily not show off ...) )
1. Initialize a webview (viewdidload)
uiwebview* WebView = [[UIWebView alloc] initWithFrame:self.view.bounds]; [Self.view Addsubview:webview];
2. Associate this webview with the Webviewjavascriptbridge (viewdidload)
if (_bridge) {return;} uiwebview* WebView = [[UIWebView alloc] initWithFrame:self.view.bounds]; [Self.view Addsubview:webview]; [Webviewjavascriptbridge enableLogging]; _bridge = [Webviewjavascriptbridge bridgeforwebview:webview webviewdelegate:self handler:^ (ID data, Wvjbresponsecallback responsecallback) { NSLog (@ "OBJC received message from JS:%@", data); Responsecallback (@ "Response for message from OBJC"); }];
PS: At this time your webview with JS on the bridge. The following is the mutual transfer of the method's mutual harmonic parameters.
(1) JS adjust OC method (can pass data to OC method value, use Responsecallback to return the value to JS)
[_bridge registerhandler:@ "Testobjccallback" handler:^ (ID data, Wvjbresponsecallback responsecallback) { NSLog (@ "testobjccallback called:%@", data); Responsecallback (@ "Response from Testobjccallback"); }];
note Here The testobjccallback of this method. The HTML side of the name will be the same as iOS, can be transferred to this method. Of course, this name can be discussed on both sides of the custom. Simple and clear.
(2) OC Tuning JS Method (through the data can be passed the value, through the response can accept JS over the return value)
ID data = @{@ "GREETINGFROMOBJC": @ "Hi there, js!"}; [_bridge callhandler:@ "Testjavascripthandler" Data:data responsecallback:^ (ID response) { NSLog (@ "Testjavascripthandler responded:%@", response); }];
Note that the Testjavascripthandler here is also a method indicator.
(3) OC to JS value (accept return value by response)
[_bridge send:@ "A string sent from OBJC to JS" responsecallback:^ (ID response) { NSLog (@ "SendMessage got response:%@", response); }];
(4) OC to JS Pass value (no return value)
[_bridge send:@ "A string sent from OBJC after Webview have loaded."];
Source: Https://github.com/marcuswestin/WebViewJavascriptBridge
Source code Principle Analysis: http://www.2cto.com/kf/201503/384998.html
Webviewjavascriptbridge Instructions for use (IOS)