Big Nerd IOS Programming 21st Web Service, UIWebView

Source: Internet
Author: User

1. Two parts: Link and get data from acquisition to data building data Model2. Send a request to the Web server Communication ' browser via HTTP protocol to the established URL address, and the URL returns to the Pleading page. HTML and image ' browser takes other parameters such as table data to the established server, the server returns a custom or dynamic Live Web page3. The data format is typically JSON or XML4. The URL format of the request. Typically there is no specific format, as long as the server can recognize it. such as Gray steaming p://Bookapi.bignerdranch.com/course.jsonhttp//Baseurl.com/servicename?argumentx=valuex&argumenty=valueyFor example, get the course content for a given time: http://bignerdranch.com/course?year=2014&month=11!!!URL address can not wrap the space and other special characters, if necessary, you want to escape NSString*search =@"Play some \"Abba\""; NSString*escaped =[Search stringbyaddingpercentescapesusingencoding:nsutf8stringencoding]; Resolve Address-(NSString *) stringbyremovingpercentencoding;5. Using Urlsession nsurlsession*session; Nsurlsessionconfiguration*config =[Nsurlsessionconfiguration defaultsessioncofiguration]; Session= [Nsurlsession sessionwithconfiguration:configDelegate: nil Delegatequeue:nil]; Create with a configuration and delegate,delegatequeue by default you can get data for nil:-(void) fetchfeed {nsstring*requeststring =@"Http://bignerdranch.com/course.json"; Nsurl*url =[Nsurl urlwithstring:requeststring]; Nsurlrequest*req =[Nsurlrequest Requestwithurl:url]; Nsurlsessiondatatask*datatask =[self.session datataskwithrequest:req Completionhandler:^ (NSData *data, Nsurlresponse *response, Nserror *error) {NSString*json =[[NSString alloc] Stringwithdata:data encoding:nsutf8stringencoding];        }];    [Datatask resume]; Use session to create Datatask6. JSON data parsing: Using native class Nsjsonserialization, the corresponding data will be converted to the NS type, Nsdictionary,nsarray,nsstring,nsnumber ...^ (NSData *data,nsurlresponse *response,nserror *error) {Nsdictionary*jsonobject =[nsjsonserialization jsonobjectwithdata:data tions:0Ror:nil]; }    //Modify Tableviewcontroller- (void) viewdidload {[Super viewdidload]; [Self.tableview Registerclass:[uitableviewcellclass] Forcellreuseidentifier:@"UITableViewCell"]; }    -(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section {return 0; return[self.courses Count]; }    -(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath*) Indexpath {returnNil; UITableViewCell*cell =[TableView dequeuereusablecellwithidentifier:@"UITableViewCell"Forindexpath:indexpath]; Nsdictionary*course =Self.courses[indexpath.row]; Cell.textLabel.text= course[@"title"]; returncell; }7The main thread main thread the modern Apple device has multiple processors, so there can be multiple code snippets executing concurrently.    The main thread is sometimes called the UI thread, and all related to the handling of the UI is placed in the main thread. However, like the one above, by default nsurlsessiondatatask is placed on other threads to handle background thread,Contrast with the above practice differences * * * *after loading reloaddata, UI-related, how to put in the main thread run: Dispatch_async function^ (NSData *data,nsurlresponse *response,nserror *error) {Nsdictionary*jsonobject =[nsjsonserialization jsonobjectwithdata:data tions:0Ror:nil]; Dispatch_async (Dispatch_get_main_queue (),^{[Self.tableview reloaddata]; })    }--------------------------------------------------------8. Each cell data before UIWebView retains a specific URL, clicking on the cell to display the contents of the URL, but does not need to leave the app to open Safari//UIWebView as V exists in a viewcontroller (c)    @interfaceBnrwebviewcontroller:uiviewcontroller @property (nonatomic) Nsur*URL; @end    @implementationBnrwebviewcontroller-(void) Loadview {UIWebView*webview =[[UIWebView alloc] init]; Webview.scalepagetofit=YES; Self.view=WebView; }    -(void) SetUrl: (Nsurl *) URL {_url=URL; if(_url) {nsurlrequest*req =[Nsurlrequest Requestwithurl:_url]; [(Uiwenview*) Self.view Loadrequest:req]; }    }    @end    //////to define a new property in BNRCourseViewController.h:@classBnrwebviewcontroller;//If you just declare, introduce a class to@property (nonatomic) Bnrwebviewcontroller*Webviewcontroller; @end//////in the delegation-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) Options {//.... code.Bnrwebviewcontroller*WVC =[[Bnrwebviewcontroller alloc] init]; Cvc.webviewcontroller=WVC; }//////    @implementationBnrcourseviewcontroller-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) Indexpath {nsdictionary*course =Self.courses[indexpath.row]; Nsurl*url = [Nsurl urlwithstring:course[@"URL"]]; Self.webViewController.title= course[@"title"]; Self.webViewController.URL=URL;    [Self.navigationcontroller PushViewController:self.webViewController Animated:yes]; }9. Certificate credentials When you visit some websites that require authorization or authentication, you cannot get data directly, and you need to provide verification in the middle.//user name and password required-(void) fetchfeed {nnstring*requeststring =@"Https://bookapi.bignerranch.com/private/courses.json"; //code ...    }    -(Instancetype) Initwithstyle: (uitableviewstype) style {//code ...        {            //code ..._session =[Nsurlsession sessionwithconfiguration:configDelegate: Self delegatequeue:nil];        [Self fetchfeed]; }        returnSelf ; }    //////    @implementationBnrcoursesviewcontroller () <NSURLSessionDataDelegate>//Delegation Method-(void) Urlsession: (Nsurlsession *) session Task: (Nsurlsessiontask *) Task Didreceivechallenge: (Nsurlauthenticationchallenge*) Challenge Completionhandler: (void(^) (Nsurlsessionauthchallengedisposition,nsurlcredential *)) Completionhandler {nsurlcredential*cred = [Nsurlcredential credentialwithuser:@"Bignerdranch"Password@"Achievenerdvana"persistence:nsurlcredentialpersistenceforsession Completionhandler (nsurlsessionauthchallengeuse        credential,cred)]; }    @endTen. UIWebView will record the browsing history, you can use Add Uitoolbar forward back [Goback,goforward]

Big Nerd IOS Programming 21st Web Service, UIWebView

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.