1. synchronous request download
The interaction between synchronous requests and users is not good, and it is prone to freezing. After sending the request, wait for the response from the server. After returning the data, perform the next step.
Create a blank view and add the following code to the (BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) lanuchOptions method in the AppDelegate. m file:
// ----- Synchronous request download // obtain the network resource path (URL) NSURL * pURL = [NSURL URLWithString: @ "http://www.baidu.com"]; // create a request NSURLRequest * pRequest = [NSURLRequest requestWithURL: pURL cachePolicy: Required timeoutInterval: 60]; // establish a connection NSURLResponse * pResponse = nil; NSError * pError = nil; // initiate a request to the server (after sending the request, the thread will wait for the server to respond until the maximum response event is exceeded). After obtaining the data, convert it to NSData type NSData * pData = [NSURLConnection sendSynchronou SRequest: pRequest returningResponse: & pResponse error: & pError]; // output data, view ,?? You can also parse the NSLog (@ "pData = % @", pData); NSLog (@ "pError = % @", [pError localizedDescription]);
2. asynchronous request download
Asynchronous light ride supports applications to download data in the background. Other operations are not affected while waiting for completion. Asynchronous requests must implement the NSURLConnectionDataDelegate Protocol to implement the method. At the same time, you must create a variable NSMutableData type object to store the downloaded data.
In the. h file, follow the protocol to create an object for data storage. The Code is as follows:
@interface LinAppDelegate : UIResponder
@property (retain, nonatomic) NSMutableData * pData;@property (strong, nonatomic) UIWindow *window;@end
Remember to release the created object.
In the. h file, implement the Protocol method to store the asynchronous request data. The Code is as follows:
-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions {self. window = [[[UIWindow alloc] initWithFrame: [UIScreen mainScreen] bounds] autorelease]; self. window. backgroundColor = [UIColor whiteColor]; // ----- asynchronous request // obtain the network resource path (URL) NSURL * pURL = [NSURL URLWithString: @ "hppt: // www.baidu.com"]; // create a request NSURLRequest * pRequest = [NSURLRequest requ according to the URL EstWithURL: pURL cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 60]; // initiate a request to complete data acquisition through the delegate mode callback [NSURLConnection connectionWithRequest: pRequest delegate: self]; [self. window makeKeyAndVisible]; return YES;} # pragma mark ----- NSURLConnectionDataDelegate // Method for server response callback-(void) connection :( NSURLConnection *) connection didReceiveResponse :( NSURLResponse *) response {NSLog (@ "server response! "); // Initialize and create the memory space self. pData = [NSMutableData dataWithCapacity: 5000];} // when the server returns data, the client starts to accept (data is returned)-(void) connection :( NSURLConnection *) connection didReceiveData :( NSData *) data {NSLog (@ "the server returns data! "); // Put the returned data into the cache [self. pData appendData: data];} // Method for callback after data reception is complete-(void) connectionDidFinishLoading :( NSURLConnection *) connection {NSLog (@ "the data has been received! "); // Output the received data NSLog (@" pData = % @ ", self. pData);} // method called when data reception fails-(void) connection :( NSURLConnection *) connection didFailWithError :( NSError *) error {NSLog (@ "data reception failed, cause of failure: % @ ", [error localizedDescription]);}