Interaction between iOS and HTML5 and JS---learning note four

Source: Internet
Author: User

The first frame is Webviewjavascriptbridge, which makes it easy for iOS and JS to interact with each other.

I. Flowchart (main introduction idea)

Two. How to use the iOS side
First Import#import "WebViewJavascriptBridge.h"file First step: Open log//open log for easy commissioning[Webviewjavascriptbridge enableLogging]; Step two: Build bridges for OBJC and JS//to which WebView build JS and OJBC Bridge of communicationSelf.bridge =[Webviewjavascriptbridgebridgeforwebview:webview];//set up the agent, if you do not need to implement, you can not set[self.bridgesetWebViewDelegate:self]; step three: Register Handlename, which is used to call the iOS side of the JS end//JS Active call OJBC method//This is JS will call the GETUSERIDFROMOBJC method, which is the OC registered to the JS call//JS needs a callback, of course, JS can also pass parameters over. Data is the parameter of JS, it is not necessarily necessary to pass//OC End through the Responsecallback callback JS end, JS can get the required data[Self.bridgeregisterhandler:@"GETUSERIDFROMOBJC"handler:^ (IDdata, Wvjbresponsecallback responsecallback) {NSLog (@"JS call GETUSERIDFROMOBJC, data from JS is%@", data); if(responsecallback) {//Feedback to JSResponsecallback (@{@"userId":@"123456"});  }}]; [Self.bridgeregisterhandler:@"GETBLOGNAMEFROMOBJC"handler:^ (IDdata, Wvjbresponsecallback responsecallback) {NSLog (@"JS call GETBLOGNAMEFROMOBJC, data from JS is%@", data); if(responsecallback) {//Feedback to JSResponsecallback (@{@"Blogname":@"Xing Zi"}); }}]; The fourth step: directly invoke the JS terminal registered Handlename [Self.bridgecallhandler:@"Getuserinfos"data:@{@"name":@"Ddsds"}responsecallback:^ (IDresponsedata) {NSLog (@"From JS:%@", ResponseData);}];

Three. How to use JS Terminal
<script>Window.onerror=function (Err) {log ('Window.onerror:'+err)} /*This code is fixed and must be placed in JS*/function Setupwebviewjavascriptbridge (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='wvjbscheme://__bridge_loaded__';        Document.documentElement.appendChild (Wvjbiframe); SetTimeout (function () {Document.documentElement.removeChild (Wvjbiframe)},0)      }          /*all JS methods that interact with OC are registered here to call OC via JS or let OC Invoke JS*/Setupwebviewjavascriptbridge (function (bridge) {var uniqueId=1function log (message, data) {var log= document.getElementById ('Log') var el= Document.createelement ('Div') El.classname='logline'el.innerhtml= uniqueid++ +'. '+ Message +':<br/>'+json.stringify (data)if(log.children.length) {Log.insertbefore (el, log.children[0])         } Else{log.appendchild (EL)}}/*Initialize your app here*/              /*Here we register a JS call oc method, without parameters, and without OBJC feedback results to JS: Open this demo corresponding blog*/Bridge.registerhandler ('openwebviewbridgearticle', function () {log ("Openwebviewbridgearticle was called with by OBJC")     })      /*JS to the OBJC to provide a public API, on the OBJC side can manually invoke the JS API. Receive OBJC passed parameters, and can callback OBJC*/-Bridge.registerhandler ('token', function (data, responsecallback) {log ("G-Star ye:", data) Responsecallback ({Here I return to the stitching address})})/*JS to OBJC provide a public API,OBJC end by registering, you can call this API on the JS side, get callback. The OBJC can be fed back to JS after processing is completed, which is called when the loading page is completed.*/Bridge.callhandler ('GETUSERIDFROMOBJC', Function (responsedata) {log ("JS call objc ' s GETUSERIDFROMOBJC function, and JS received response:", ResponseData)}) document.getElementById ('Blogids'). onclick =function (e) {log ('JS Call OBJC:GETBLOGNAMEFROMOBJC') Bridge.callhandler ('ww', {'Blogdurl':'WEIDSFDL'}, function (response) {log ('JS got response', Response)}) }     })           </script>
Four. Precautions

1. When such a hint appears, the JS side has implemented the name of the "WW" method, but the iOS side is not registered,

The specific implementation is:
[self.bridgeregisterhandler:@ "WW" handler:^ (ID data, wvjbresponsecallback responsecallback) { NSLog (@ "JS Call GETBLOGNAMEFROMOBJC, data from JS is%@ ", data); If (responsecallback) { //feedback to JS responsecallback (@{@ "blogname": @ "star"});} ];
Five. To prevent WebView long press the implementation method is as follows:
-(void) Webviewdidfinishload: (UIWebView *) webview{    //  Disable page element selection    [ WebView stringbyevaluatingjavascriptfromstring:@ "document.documentelement.style.webkituserselect=" None '; " ];     //      Disable long press eject Actionsheet    [WebView stringbyevaluatingjavascriptfromstring:@ "  Document.documentelement.style.webkittouchcallout= ' None '; " ];}



Interaction between iOS and HTML5 and JS---Learning note four

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.