Synchronous download, the main code of the synchronization request is as follows
-(ibaction) DownLoad: (ID) sender {
nsstring *[email protected] "http://7jpnsh.com1.z0.glb.clouddn.com/TravelDemo.plist"; file Address
nsurl *url=[nsurl urlwithstring:urlasstring];
Nsurlrequest *request=[nsurlrequest requestwithurl: url];
nsmutabledata *error=nil;
nsdata *data=[nsurlconnection sendsynchronousrequest: Request Returningresponse:Nil error:&error];
File Save Directory
nsstring *cachepath=[nssearchpathfordirectoriesindomains(nsdocumentdirectory, Nsuserdomainmask, YES) lastobject];
NSLog (@ "%@", CachePath);
nsstring *filename=[cachepath stringbyappendingpathcomponent:@ "Traveldemo.plist"];
/* Data downloaded * /
if (data!=nil) {
NSLog (@ " download succeeded ");
if ([Data writetofile:filename atomically:YES]) {
NSLog (@ " Save succeeded ");
}Else
{
NSLog (@ " save failed ");
}
}else{
NSLog (@ "%@", error);
}
}
Asynchronous download, the code for the asynchronous request is as follows
-(ibaction) DownLoad: (ID) sender {
nsstring *[email protected] "http://7jpnsh.com1.z0.glb.clouddn.com/TravelDemo.plist";
nsurl *url=[nsurl urlwithstring:urlasstring];
Nsurlrequest *request=[nsurlrequest requestwithurl: url];
Nsmutabledata*data=[[nsmutabledata alloc]init];
self. Connectiondata=data;
[Data release];
nsurlconnection *newconnection=[[nsurlconnection alloc]initwithrequest: Request delegate :self startimmediately:YES];
/**
The connection and Conectiondata types are as follows:
nsurlconnection *connection;
Nsmutabledata *connectiondata;
**/
Self . connection=newconnection;
if (self. connection!=nil) {
NSLog (@ " successfully created Connection ");
}else{
NSLog (@ " Create Connection Failed ");
}
}
Asynchronous Download Agent method
-(void) connection: (nsurlconnection*) connection didfailwitherror: (nserror *) error{
NSLog (@ " error ");
NSLog (@ "%@", error);
}
-(void) connection: (nsurlconnection*) connection didreceivedata: (nsdata *) data{
NSLog(@ "Received data");
[Self. connectiondata appenddata:data];
}
-(void) connectiondidfinishloading: (nsurlconnection*) connection{
NSLog (@ " download succeeded ");
// File save directory
nsstring *cachepath=[nssearchpathfordirectoriesindomains(nsdocumentdirectory, Nsuserdomainmask, YES) lastobject];
NSLog (@ "%@", CachePath);
nsstring *filename=[cachepath stringbyappendingpathcomponent:@ "Traveldemo.plist"];
if ([Self. Connectiondata writetofile:filename atomically:YES]) {
NSLog (@ " Save succeeded ");
}else{
NSLog (@ " save failed ");
}
}
Add:
Connectionwithrequest need delegate parameters, through a delegate to do the data download and request acceptance and connection status, here delegate:self, so need to implement some methods of this class, Also define mdata to do the acceptance of the data.
Methods that need to be implemented:
1, get the return status, Baotou information.
-(void) connection: (Nsurlconnection *) connection didreceiveresponse: (Nsurlresponse *) response;
2, connection failure, including failure.
-(void) connection: (Nsurlconnection *) connection didfailwitherror: (Nserror *) error;
3. Receive data
-(void) connection: (Nsurlconnection *) connection didreceivedata: (NSData *) data;
4. Data reception Complete
-(void) connectiondidfinishloading: (nsurlconnection *) connection;
Synchronizing request data can cause the main thread to block, and it is often not recommended when big data is requested or the network is not smooth.
As you can see from the code above, the steps to establish communication are basically the same regardless of the synchronous request or the asynchronous request:
1. Create Nsurl
2. Create a Request object
3. Create a nsurlconnection connection.
Once the nsurlconnection is created, an HTTP connection is created. The difference between an asynchronous request and a synchronous request is that an asynchronous request is created, the user can do other things, the request is executed on another thread, and the communication results and procedures are executed in the callback function. Synchronization requests are different and require the end user to be requested to do other things.
iOS comes with nsurlconnection download file