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!
-
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