当用户按下home键时,app进入后台,此时app的下载过程会自动暂停,直到重新激活app,下载会自动恢复
e.g.
- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
NSString *urlAsString = @"http://www.apple.com";
NSURL *url = [NSURL URLWithString:urlAsString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection
sendAsynchronousRequest:urlRequest
queue:queue
completionHandler:^(NSURLResponse *response,
NSData *data,
NSError *error) {
if (error == nil && [data length] >0 ){
NSLog(@"have data");
}else if (error == nil && [data length] == 0){
NSLog(@"No data");
}else if (error != nil){
NSLog(@"Error happened");
}
}];
return YES;
}
e.g.
- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
dispatch_queue_t dispatchQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(dispatchQueue, ^(void) {
NSString *urlAsString = @"http://www.apple.com";
NSURL *url = [NSURL URLWithString:urlAsString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
NSError *error = nil;
NSData *connectionData = [NSURLConnection
sendSynchronousRequest:urlRequest
returningResponse:nil
error:&error];
if (error == nil && [connectionData length] >0 ){
NSLog(@"have data");
}else if (error == nil && [connectionData length] == 0){
NSLog(@"No data");
}else if (error != nil){
NSLog(@"Error happened");
}
});
return YES;
}