This time, we mainly talk about the process of creating a connection and downloading data by specifying the corresponding URL address, sending a request, we do not care about the downloaded content. We will not process the downloaded content for the time being. We will leave it for future solutions. First, we should create an empty view project. There are two ways to send requests during encoding: Synchronous requests and asynchronous requests. asynchronous requests are more common in daily life than synchronous requests. So let's talk about asynchronous requests first.
The Code is as follows:
HHLAppDelegate. h
#import
@interface BoAppDelegate : UIResponder
@property (strong, nonatomic) UIWindow *window;@property (retain, nonatomic) NSMutableData *pData;@end
HHLAppDelegate. m
# Import "BoAppDelegate. h "# define URL @" http://www.baidu.com "@ implementation BoAppDelegate-(void) dealloc {[_ window release]; [_ pData release]; [super dealloc];}-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions {self. window = [[[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds] autorelease]; // Override point for customization after application launch. self. window. backgroundColor = [UIColor whiteColor];/* asynchronous request * // 1. obtain the network resource path (URL) NSURL * pURL1 = [NSURL URLWithString: URL]; // 2. create a request NSURLRequest * pRequset1 = [NSURLRequest requestWithURL: pURL1 cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 60] According to the URL; // 3. (difference from Synchronous requests) Initiate a request and call back in the delegate mode to obtain data [NSURLConnection connectionWithRequest: pRequset1 delegate: self]; [self. window makeKeyAndVisible]; return YES;} # pragma mark URLConnectionDataDelegate // 1. server response callback method-(void) connection :( NSURLConnection *) connection didReceiveResponse :( NSURLResponse *) response {NSLog (@ "server response"); self. pData = [NSMutableData dataWithCapacity: 5000];} // 2. when the service returns data, the client starts to accept (data is the returned data)-(void) connection :( NSURLConnection *) connection didReceiveData :( NSData *) data {NSLog (@ "server return data "); // put the returned data in the cache [self. pData appendData: data];} // 3. method for callback after data is accepted-(void) connectionDidFinishLoading :( NSURLConnection *) connection {NSLog (@ "data accepted"); NSLog (@ "pData = % @", self. pData);} // 4. method called when data reception fails-(void) connection :( NSURLConnection *) connection didFailWithError :( NSError *) error {NSLog (@ "data reception failed, cause of failure: % @", [error localizedDescription]);}-(void) applicationWillResignActive :( UIApplication *) application {// Sent when the application is about to move from active to inactive state. this can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. games shocould use this method to pause the game .} -(void) applicationDidEnterBackground :( UIApplication *) application {// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits .} -(void) applicationWillEnterForeground :( UIApplication *) application {// Called as part of the transition from the background to the inactive state; here you can undo records of the changes made on entering the background .} -(void) applicationDidBecomeActive :( UIApplication *) application {// Restart any tasks that were paused (or not yet started) while the application was inactive. if the application was previusly in the background, optionally refresh the user interface .} -(void) applicationWillTerminate :( UIApplication *) application {// Called when the application is about to terminate. save data if appropriate. see also applicationDidEnterBackground :.} @ end
The running result is as follows:
The following figure shows the downloaded data. It looks like this because it has not been processed.
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + Cs2ssr3Ktc/Signature + signature + CjxwPgrI58/Co7o8L3A + CjxwPgo8YnI + cda-vcd4kpha + Signature = "brush: java;">
# Import "BoAppDelegate. h "# define URL @" http://www.baidu.com "@ implementation BoAppDelegate-(void) dealloc {[_ window release]; [_ pData release]; [super dealloc];}-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions {self. window = [[[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds] autorelease]; // Override point for customization after application launch. self. window. backgroundColor = [UIColor whiteColor]; // synchronous request // Step 1: url nsurl * pURL = [NSURL URLWithString: URL]; // Step 2: create a request NSURLRequest * pRequest = [NSURLRequest requestWithURL: pURL cachePolicy: Required timeoutInterval: 60]; // Step 3: establish a connection NSError * pError = nil; NSURLResponse * pRespond = nil; // initiate a request to the server (the thread will wait for the server to respond until the maximum response time is exceeded) NSData * pData = [NSURLConnection sendSynchronousRequest: pRequest returningResponse: & pRespond error: & pError]; NSLog (@ "pData = % @", pData); NSLog (@ "pError = % @", [pError localizedDescription]); /// * asynchronous request * // 1. obtain the network resource path (URL) // NSURL * pURL1 = [NSURL URLWithString: URL]; // 2. create a request according to the URL // NSURLRequest * pRequset1 = [NSURLRequest requestWithURL: pURL1 cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 60]; // 3. (difference from Synchronous requests) Initiate a request and call back in the delegate mode to obtain data // [NSURLConnection connectionWithRequest: pRequset1 delegate: self]; [self. window makeKeyAndVisible]; return YES;}-(void) applicationWillResignActive :( UIApplication *) application {// Sent when the application is about to move from active to inactive state. this can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. games shocould use this method to pause the game .} -(void) applicationDidEnterBackground :( UIApplication *) application {// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits .} -(void) applicationWillEnterForeground :( UIApplication *) application {// Called as part of the transition from the background to the inactive state; here you can undo records of the changes made on entering the background .} -(void) applicationDidBecomeActive :( UIApplication *) application {// Restart any tasks that were paused (or not yet started) while the application was inactive. if the application was previusly in the background, optionally refresh the user interface .} -(void) applicationWillTerminate :( UIApplication *) application {// Called when the application is about to terminate. save data if appropriate. see also applicationDidEnterBackground :.} @ end
The effect is similar to that of asynchronous download above, so it won't work. Here we mainly implement the download function, which is not processed for data. I hope you will forgive me.