1. synchronous download (poor interaction, prone to freezing, usually used only when downloading data is small or has specific requirements ). After a synchronous request is sent, the program stops user interaction until the server returns data.
Steps:
1. Create an nsurl
Nsurl * url = [[nsurl alloc] initwithstring: @ "http://www.baidu.com/"];
2. Create nsurlrequest through URL
Nsurlrequest * request = [[nsurlrequest alloc] initwithurl: URL cachepolicy: nsurlrequestuseprotocolcachepolicy timeoutinterval: 15];
Cachepolicy Cache Protocol is an enumeration type:
Nsurlrequestuseprotocolcachepolicy Basic Policy
Nsurlrequestreloadignoringlocalcachedata ignore local cache
Nsurlrequestreturncachedataelseload first uses the cache. If there is no local cache, It is downloaded from the original address.
Nsurlrequestreturncachedatadontload uses the local cache and never downloads it. If the local cache does not exist, the request fails. This policy is mostly used for offline operations.
Nsurlrequestreloadignoringlocalandremotecachedata ignores any Cache Policy. Whether it is local or remote, it is always downloaded from the original address.
Nsurlrequestreloadrevalidatingcachedata is not downloaded if the local cache is valid. Download again from the original address in any other situation
3. Establish a network connection nsurlconnection to synchronize request data
Nsdata * receiveddata = (nsmutabledata *) [nsurlconnection sendsynchronousrwquest: Request returningresponse: & response error: & error];
After the preceding three steps, you need to parse the receiveddata, which is generally XML/JSON.
Ii. asynchronous download
Steps:
The first two steps are the same as the synchronization method. When receiving data in step 3, they are different from the synchronization method. Four network connection proxy methods are required.
1. Create an nsurl
Nsurl * url = [[nsurl alloc] initwithstring: @ "http://www.baidu.com/"];
2. Create nsurlrequest through URL
Nsurlrequest * request = [[nsurlrequest alloc] initwithurl: URL cachepolicy: nsurlrequestuseprotocolcachepolicy timeoutinterval: 15];
3. Establish a network connection nsurlconnection and set its proxy
[Nsurlconnection connetionwithrequest: Request delegate: Self];
Proxy to be implemented:
Nsurlconnectiondatadelegate, nsurlconnectiondelegate
The following four proxy methods are used:
// This method is called when receiving server responses
-(Void) connetion :( nsurlconnetion *) connection didreceiveresponse :( nsurlresponse *) Response
{
// Initialize receivedata, which is used to store data from the server
Self. receiveddata = [nsmutabledata data];
}
// Called when receiving data transmitted by the server. This method is executed several times based on the data size.
-(Void) connection :( nsurlconnection *) connetion didreceivedata :( nsdata *) data
{
[Self. receiveddata appenddata: Data];
}
// Call this method after data transmission.
-(Void) connetiondidfinishloading :( nsurlconneciton *) connetion
{
// Todo get the final receiveddata
}
// Any errors, such as network disconnection and connection timeout, occur during the network request process.
-(Void) connetion :( nsurlconnection *) connetion didfailwitherror :( nserror *) Error
{
Nslog (@ "% d @", [error localizeddescription]);
}