JS code and native iOS code for interactive use Webviewjavascriptbridge very simplified our operation especially on iOS side
There are a few things to note when you drop the native code with iOS:
1. js and iOS define each other's function names to be dropped with each other
2. Actions in iOS:
A. Create a bridge object
Bridgeforwebview:webview];
B. Register the code for JS callback in iOS
[Self.bridgeRegisterhandler:@ "OBJC echo" handler:^ (id data, Wvjbresponsecallback responsecallback) {nslog (@ "OBJC Echo called with: %@ B1, or invoke the method in JS [self.bridge callhandler:@ "JS echo" data:nil responsecallback:^ (id responsedata) {nslog (@ "OBJC received response: %@", responsedata);}];
3, the operation of JS code:
1. Copy the function into the JS code
functionSetupwebviewjavascriptbridge (Callback) {if (Window.Webviewjavascriptbridge) {ReturnCallback (Webviewjavascriptbridge); }if (Window.Wvjbcallbacks) {ReturnWindow.Wvjbcallbacks.Push (callback); }Window.Wvjbcallbacks= [callback];var wvjbiframe= document. createelement ( ' Iframe Wvjbiframe. style. display = ' None wvjbiframe. src = https://__bridge_loaded__ document. documentelement. appendchild (wvjbiframe); settimeout (function () {document. DocumentElement. removechild (Wvjbiframe)}, 0)}
2, in the following function to do all of your actions to interact with the app and the time of the register in the function, it is important to note that if your execution function is outside of this func, you should manually drop the execution function you want to use in the callback of this function. Case: We do JS colleague think the data changed after the JS page will automatically refresh, however, there is no, so this process is best to display in the process of the callback.
Setupwebviewjavascriptbridge (functionBridge) {/* Initialize your app here */Bridge.registerhandler (", Span class= "Pl-k" >function (data, responsecallback) { console. log ( "JS Echo called With:", data) responsecallback (data)
}) bridge. callhandler ( ' OBJC echo ", { "Key : ' Valuefunction responsecallback ( responsedata) {
/span>
Anotheroperation ()
< Span class= "Pl-smi" > < Span class= "Pl-en" > Console. Log ( "JS received Response:", ResponseData)})
})
Func anotheroperation () {
}
Webviewjavascriptbridge using JS to tune iOS native code