iOS development, interaction between UIWebView and H5

Source: Internet
Author: User

Introduction: With the continuous promotion, in the process of mobile development of the Central Plains and H5 mixed development in mobile development occupies an increasingly important position; let me share with you my experience in mixed development!

    1. To load an HTML page into the UIWebView control:

A: Local Loading:

NSString *webpath = [[nsbundlemainbundle]pathforresource:@ "Helloword" oftype:@ "html"];//get file path nsurl *webURL = [ nsurlfileurlwithpath:webpath];//set Urlnsurlrequest *urlrequest = by file path string = [nsurlrequestrequestwithurl:weburl];// Set up related Url[self.webview loadrequest:urlrequest];//submit requests for request submission

B: Loading HTML pages from the server

NSString *urlstr = [NSString stringwithformat:@ "%@/appservice/getannouncementdetail.do?id=%@", O2O_URL,self.idStr];    Nsurl *url = [Nsurl urlwithstring:urlstr];    NSLog (@ "url:%@", url);    Nsurlrequest *request = [Nsurlrequest Requestwithurl:url]; [Self.webview Loadrequest:request];


2. Join the uiwebviewdelegate delegate (if you want to do the corresponding data interaction you must add the corresponding I delegate)

-(BOOL) WebView: (UIWebView *) WebView shouldstartloadwithrequest: (nsurlrequest*) Request Navigationtype: ( Uiwebviewnavigationtype) run when the JS action of the navigationtype//html page is triggered
-(void) Webviewdidfinishload: (UIWebView *) WebView//html triggered after loading

3.OC submits JS request. Run JS Method

[self.webviewstringbyevaluatingjavascriptfromstring:@ "Method name (%@)", parameter]];


4.JS Submit request to OC

-(BOOL) WebView: (uiwebview*) Webview shouldstartloadwithrequest: ( nsurlrequest*) Request navigationtype: (Uiwebviewnavigationtype) navigationtype{     nsstring *requeststring = [[requesturl]absolutestring];//gets the absolute path of the request .     nsarray *components = [requeststringcomponentsseparatedbystring:@ ":"];//split parameter delimiter when submitting a request      if  ([componentscount] >1 && [(nsstring *) [ componentsobjectatindex:0]isequaltostring:@ "TestApp"])  {//the filter request is what we need. Unwanted requests do not enter the condition        if ([(nsstring *) [components objectatindex:1]isequaltostring:@ "Change"])   What to do with      {//      }    returnNO;     }    returnyes;} 


5. js notation in HTML5

1. Method of receiving a request

Functionfund () {//action}


2. How to submit a request

function SendCommand (cmd,param) {var url= "TestApp:" +cmd+ ":" +param;//combination request String document.location = url;//submitted Request}


Finally attach an example program (transfer array):

http://download.csdn.net/detail/fqcl992/6929375


iOS development, interaction between UIWebView and H5

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.