iOS Development--Network Programming OC Chapter & Using WebView to build Hybird applications

Source: Internet
Author: User

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

Two: 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 ("!* ' ();: @&amp;=+$,/?%#[]"),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

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.