Data download for iOS development basics

Source: Internet
Author: User

1 Data download

Brief introduction:

(1) In this paper, the author will bring you some basic knowledge of the network, nsurlconnection the way to download data from the network, and how to use the synchronization, asynchronous download display pictures and data.

1 Network Basics

What is a Web application?

1 Network application software is a variety of online business application systems developed by users, according to their needs, using software development platform. Common development platform has a variety of database management system, office automation management system and browser, Web site management and other software.

2 Client and service side

Network application is different from local application, network application data is downloaded from the network, so need to run a program on the network to provide data for the application, or provide services, then this network application is generally called the client, and the service running on the network is called the server

3IOS Network Interface Form

IOS can be used in three types of API interface for network programming, according to the level of abstraction from low to high, such as socket, stream mode, url method. Here I introduce you to the URL programming method.

(1) URL programming mode

URL Programming mode for network programming through URLs, any network resources to be accessed (including LAN and WAN) can be represented and accessed by a URL, and support resource sharing between devices. The URL Programming mode system provides five kinds of protocols, such as HTTP, HTTPS, file, FTP, data, etc., and allows the user to develop and register related classes to support additional application-layer network protocols for protocol expansion.

(4) Common data formats

There are two types of data formats commonly found in iOS development, one in JSON format and the other in XML format, with relatively more JSON format

(5) General process of interface development

Developing an interface in iOS that requires interfaces, interface assets, and network interfaces

The process of development is generally as follows

1, download data

2, parsing JSON or XML data, creating a data model

3, use the control to display data, when necessary to customize the view, such as custom cell

The URL programming pattern can be used on iOS systems with two programming interfaces: Nsurlsession and nsurlconnection. The use of Nsurlconnection is described here

//use URL address in HTTP//http://Address Usage Protocol (ftp://)    //iappfree.candou.com host address (domain name and IP)//: 8080 Host port///free/applications/limited Web program file path//currency=rmb&page=1&category_id= Program parameters (Parameters & Segmentation)NSString*urlstring =@"http://iappfree.candou.com:8080/free/applications/limited?currency=rmb&page=1&category_id="; //Download Data//generating Nsurl objects from addressesNserror *error =Nil; Nsurl*url =[Nsurl urlwithstring:urlstring]; NSString*content = [[NSString alloc] Initwithcontentsofurl:url encoding:nsutf8stringencoding error:&ERROR]; if(error==Nil) {NSLog (@"content =%@", content); }    Else{NSLog (@"Download Failed"); }        //How to use the project//1. Synchronous download, Initwithcontentsofurl will not be returned until download is finished//causes the interface to feign death, cannot use//2. Using asynchronous Download (nsurlconnection asynchronous download)

1NSURL simultaneous Download data

NSString *urlstring = @ "http://iappfree.candou.com:8080/free/applications/limited?currency=rmb&page=1& Category_id= ";        Send a Sync URL request    //nsurlrequest URL Request object    nsurl *url = [Nsurl urlwithstring:urlstring];    Nsurlrequest *request = [Nsurlrequest requestwithurl:url];    Nserror *error = nil;    NSData *data = [nsurlconnection sendsynchronousrequest:request returningresponse:nil error:&error];    if (Error = = nil)    {        NSString *str = [[NSString alloc] Initwithdata:data encoding:nsutf8stringencoding];        NSLog (@ "str =%@", str);    }    else    {        NSLog (@ "Download Failed");    }

2NSURL Asynchronous Download Data

#pragma mark-Asynchronous download-(void) testnsurlconnectionasyncdownloaddata{nsstring *urlstring = @ "http://iappfree.candou.com        : 8080/free/applications/limited?currency=rmb&page=1&category_id= ";    Initialize _data = [[Nsmutabledata alloc] init]; Initiated an asynchronous URL connection request//async: After executing the method to start the download, the immediate return//download process is performed in the background (multithreading) _connection = [[Nsurlconnection alloc]    Initwithrequest:[nsurlrequest Requestwithurl:[nsurl urlwithstring:urlstring]] delegate:self StartImmediately:YES];    NSLog (@ "initwithrequest execution complete");    }//Proxy method: Received server response execution-(void) connection: (Nsurlconnection *) connection didreceiveresponse: (Nsurlresponse *) response{ NSLog (@ "Received server response Execution");}        Proxy method: When the data is received execution//NOTE: When the data is larger, may be executed multiple times-(void) connection: (Nsurlconnection *) connection didreceivedata: (NSData *) data{ [_data appenddata:data];} Proxy method: Data download complete-(void) connectiondidfinishloading: (nsurlconnection *) connection{nsstring *str = [[NSString alloc] Init    Withdata:_data encoding:nsutf8stringencoding]; NSLog (@ "str =%@",STR); }-(void) connection: (Nsurlconnection *) connection didfailwitherror: (Nserror *) error{NSLog (@ "error =%@", error);}

Data download for iOS development basics

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.