iOS and H5 interface Jsbridge Interactive Demo

Source: Internet
Author: User

iOS and H5 interface Jsbridge Interactive Demo

Recently the company needs to add activities and news module, boss look at the same design manuscript, we ios do it again, Android do it again, the small program to do again; So decided to use the H5 page. But we native not only load the URL on the line, but also need to interact with H5, Android brother and I slowly pits ...

I used a library (Gcwebviewjsbridge-ios), GitHub website: github.com/wheying/gcwebviewjsbridge-ios

His demo is not easy to understand, I have a face to see, I wrote a simple and clear demo

Start with a good job and build some basic views
getting ready to work with iOS
//1. New WebViewSelf.webview =[[UIWebView alloc] initWithFrame:self.view.bounds]; [Self.view AddSubview:self.webView];//2. Loading Web pagesNSString *indexpath = [[NSBundle mainbundle] Pathforresource:@"Index"OfType:@"HTML"]; NSString*apphtml =[NSString Stringwithcontentsoffile:indexpath encoding:nsutf8stringencoding Error:nil]; Nsurl*baseurl =[Nsurl Fileurlwithpath:indexpath]; [Self.webview loadhtmlstring:apphtml Baseurl:baseurl];//3. Turn on the log[Gcwebviewjsbridge setenablelogging];//4. Establish a communication bridge between JS and OC for WebView_bridge =[Gcwebviewjsbridge BridgeForWebView:self.webView]; [_bridge setwebviewdelegate:self];

JS Ready to work

Before it was only a set, found iOS line Android and not; Android line up iOS again. I finally knew I had to write two sets.

//This code is fixed and must be placed in JSfunctionSetupwebviewjavascriptbridge (callback) {//Android   if(window. Gcwebviewandroidjsbridge) {callback (Gcwebviewandroidjsbridge)}Else{Document.addeventlistener (' Gcwebviewandroidjsbridgeready '         , function() {Callback (Gcwebviewandroidjsbridge)},false     ); }   //IOS   if(window. Gcwebviewjsbridge) {returncallback (Gcwebviewjsbridge); }   if(window. Kbwvjsbcallbacks) {returnwindow.   Kbwvjsbcallbacks.push (callback); } window. Kbwvjsbcallbacks=[callback]; varGcwvjsbiframe = document.createelement (' iframe '); GCWVJSBIframe.style.display= ' None '; GCWVJSBIFRAME.SRC= ' gcwvjsbscheme://__gc_bridge_loaded__ ';   Document.documentElement.appendChild (Gcwvjsbiframe); SetTimeout (function() {Document.documentElement.removeChild (gcwvjsbiframe)}, 0);}
I. iOS registration method provided to JS call
iOS Registration Access album Method
[_bridge Registerobjchandler:@ "opencamera" handler:^ (ID  data, Gcwvjsbresponsecallback responsecallback) {    NSLog (@ " need%@ picture ", data[@ " Count " ]);     *IMAGEVC = [[Uiimagepickercontroller alloc] init];     = uiimagepickercontrollersourcetypephotolibrary;    [Self PRESENTVIEWCONTROLLER:IMAGEVC animated:yes completion:nil];}];

JS calls the code snippet of the method

JS Call Open album Method
function Responsecallback (responsedata) {   console.log ("parameters returned in OC:", ResponseData)});
Two. JS registration method provided to iOS call
iOS Registration provides user information method
/*JS provides the public API for OC, which can be called manually in OC, and can receive parameters passed in OC, and can callback OC*///Get user InformationBridge.registerhandler ('GetUserInfo', function (data, responsecallback) {Console.log ("parameters passed over in OC:", data); //return the processed results to OC.Responsecallback ({"UserID":"DX001","UserName":"The spinning of China"," Age":" -","Othername":"The spinning of China"})});

iOS calls the method in JS to get user information

//invoke the API in JS[Self.bridge Callhandler:@"GetUserInfo"data:@{@"userId":@"DX001"} responsecallback:^ (IDresponsedata) {NSString*userinfo = [NSString stringWithFormat:@"%@, Name:%@, Age:%@", responsedata[@"UserID"], responsedata[@"UserName"], responsedata[@" Age"]];}];

The article is not written in detail, you can follow my demo directly read the code: Demo

Ah ~ this way to achieve, the need for three-party coordination test, really is very troublesome ah ~

iOS and H5 interface Jsbridge Interactive Demo

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.