Webviewjavascriptbridge using JS to tune iOS native code

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.