Build Hybird Apps with WebView
Hybird is a combination of local technology and the web, enabling cross-platform mobile application development, one of the most commonly used frameworks: PhoneGap
A: First of all, write good HTML code
1<! DOCTYPE html>234<meta name="Viewport"Content="width=device-width, Height=device-height, initial-scale=1.0, maximum-scale=1.0, User-scalable=no;"/>5<meta http-equiv="Content-type"Content="text/html; Charset=utf-8"/>6<script>7 //calling from an iOS object8 function HelloWorld (msg) {9document.getElementById ('message'). InnerHTML =msg;Ten } One //calling iOS objects A function Showandroiddialog (msg) { -var myjsonobject =NewObject (); -Myjsonobject.title ='HelloWorld'; theMyjsonobject.message =msg; -var jsonstring =json.stringify (myjsonobject); -var uri ='gap://xxxclass.xxxmethod#'+jsonstring; -Window.location =URI; + } -</script> + A at<body> - -<button onclick='Showandroiddialog ("JS to IOS object")'> Call iOS Objects </button> -<br><br> -<divID='message'></div> -</body> inTwo: Call JavaScript code in iOS:
1- (void) Viewdidload2 {3 [Super Viewdidload];4 //additional setup after loading the view, typically from a nib.5 6Self.webview.Delegate=Self ;7NSString *path = [[NSBundle mainbundle] Pathforresource:@"Www/index"OfType:@"HTML"];8 [Self.webview loadrequest:[nsurlrequest Requestwithurl:[nsurl Fileurlwithpath:path]];9 }Ten One- (void) didreceivememorywarning A { - [Super didreceivememorywarning]; - //Dispose of any resources the can be recreated. the } - - #pragmaMark Uiwebviewdelegate - +- (void) Webviewdidfinishload: (UIWebView *) WebView - { +[Self.webviewstringbyevaluatingjavascriptfromstring:@"HelloWorld (' Invoke JS Ok from iOS object. ')"]; A } at - --(BOOL) WebView: (UIWebView *) WebView shouldstartloadwithrequest: (Nsurlrequest *Request Navigationtype: (uiwebviewnavigationtype) Navigationtype - { -NSString *actiontype =request. Url.host; -NSString *scheme =request. Url.scheme; inNSString *fragment =[Request. Url.fragment urldecodedstring]; -NSData *responsedata =[Fragment datausingencoding:nsutf8stringencoding]; to + if([Scheme isequaltostring:@"Gap"] ) { - if([ActionType isequaltostring:@"Xxxclass.xxxmethod"]) { the *nserror*error; $nsdictionary* JSON =[NsjsonserializationPanax Notoginseng Jsonobjectwithdata:responsedata - options:nsjsonreadingallowfragments theerror:&ERROR]; + ANSLog (@"title:%@, message:%@", [JSON Objectforkey:@"title"], [JSON Objectforkey:@"message"] ); the + } - } $ return true; $}
Note: Here's a nsstring classification used to encode and decode string rain URLs
1-(NSString *) urlencodedstring2 {3NSString *result = (NSString *)4 cfbridgingrelease (Cfurlcreatestringbyaddingpercentescapes (Kcfallocatordefault,5 (Cfstringref) Self,6 NULL,7CFSTR ("!* ' ();: @&=+$,/?%#[]"),8 kCFStringEncodingUTF8));9 returnresult;Ten } One A-(nsstring*) urldecodedstring - { -NSString *result = (NSString *) the cfbridgingrelease (cfurlcreatestringbyreplacingpercentescapesusingencoding (Kcfallocatordefault, - (Cfstringref) Self, -CFSTR (""), - kCFStringEncodingUTF8)); + returnresult; -}
iOS Development--Network Programming OC Chapter & Using WebView to build Hybird applications