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 ([data length] > 0 && error == nil) {
// NSString* html = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
// NSLog(@"HTML = %@",html);
/* Get the documents directory */
NSString* documentsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
/* Append the file name to the documents directory */
NSString* filePath = [documentsDir stringByAppendingPathComponent:@"apple.html"];
/* Write the data to the file */
[data writeToFile:filePath atomically:YES];
NSLog(@"Successfully saved the file to %@",filePath);
}else if ([data length] == 0 && error == nil) {
NSLog(@"Nothing was download");
}else if (error != nil) {
NSLog(@"Error happened = %@",error);
}
}];
IOS 异步请求
最新推荐文章于 2023-03-14 16:45:51 发布