为了学习iOS开发的网络知识,写了个demo对比原生和第三方网络库的用法。
NSURLConnection在iOS9被宣布弃用,NSURLSession是苹果在iOS7后为HTTP数据传输提供的一系列接口,比NSURLConnection强大,好用.
// Simple Get - (void)httpGetTest { // Any url will be ok, here we use a little more complex url and params NSString *httpUrl = @"http://apis.baidu.com/thinkpage/weather_api/suggestion"; NSString *httpArg = @"location=beijing&language=zh-Hans&unit=c&start=0&days=3"; NSURLSession *session = [NSURLSession sharedSession]; NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@?%@", httpUrl, httpArg]]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; // Add some extra params [request setHTTPMethod:@"GET"]; [request addValue:@"7941288324b589ad9cf1f2600139078e" forHTTPHeaderField:@"apikey"]; // Set the task, we can also use dataTaskWithUrl NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { // Do sth to process returend data if(!error) { NSLog(@"%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]); } else { NSLog(@"%@", error.localizedDescription); } }]; // Launch task [dataTask resume]; }
// Simple Post - (void)httpPostTest { NSURL *url = [NSURL URLWithString:@"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; request.HTTPMethod = @"POST"; request.HTTPBody = [@"date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213" dataUsingEncoding:NSUTF8StringEncoding]; NSURLSession *session = [NSURLSession sharedSession]; NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { // Do sth to process returend data if(!error) { NSLog(@"%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]); } else { NSLog(@"%@", error.localizedDescription); } }]; [dataTask resume]; }
// Simple Download - (void)downLoadTest { NSURL *url = [NSURL URLWithString:@"http://pic1.desk.chinaz.com/file/10.03.10/5/rrgaos56_p.jpg"]; NSURLSession *session = [NSURLSession sharedSession]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; // Use download with request or url NSURLSessionDownloadTask *downloadPhotoTask = [session downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) { // Move the downloaded file to new path NSString *path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:response.suggestedFilename]; [[NSFileManager defaultManager] moveItemAtURL:location toURL:[NSURL fileURLWithPath:path] error:nil]; // Set the image UIImage *downloadedImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:location]]; dispatch_async(dispatch_get_main_queue(), ^{ NSLog(@"set the image"); // self.imageView.image = downloadedImage; // set image from original location self.imageView.image = [UIImage imageWithContentsOfFile:path]; // set image from file system }); }]; [downloadPhotoTask resume]; }
// Simple Upload - (void)upLoadTest { NSURLSession *session = [NSURLSession sharedSession]; // Set the url and params NSString *urlString = @"http://www.freeimagehosting.net/upload.php"; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]]; [request addValue:@"image/jpeg" forHTTPHeaderField:@"Content-Type"]; [request addValue:@"text/html" forHTTPHeaderField:@"Accept"]; [request setHTTPMethod:@"POST"]; [request setCachePolicy:NSURLRequestReloadIgnoringCacheData]; [request setTimeoutInterval:20]; //Set data to be uploaded UIImage *image = [UIImage imageNamed:@"beauty.jpg"]; NSData *data = UIImageJPEGRepresentation(image, 1.0); // Use download from data or file NSURLSessionUploadTask *uploadPhotoTask = [session uploadTaskWithRequest:request fromData:data completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { // Do sth to process returend data if(!error) { NSLog(@"upload success"); NSLog(@"%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]); } else { NSLog(@"%@", error.localizedDescription); } }]; [uploadPhotoTask resume]; }
// Url session delegate - (void)UrlSessionDelegateTest { NSString *httpUrl = @"http://apis.baidu.com/thinkpage/weather_api/suggestion"; NSString *httpArg = @"location=beijing&language=zh-Hans&unit=c&start=0&days=3"; // Set session with configuration, use this way to set delegate NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[[NSOperationQueue alloc] init]]; NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@?%@", httpUrl, httpArg]]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; // Add some extra params [request setHTTPMethod:@"GET"]; [request addValue:@"7941288324b589ad9cf1f2600139078e" forHTTPHeaderField:@"apikey"]; // Set the task NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request]; [dataTask resume]; } - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler { // Must allow the response of server, then we can receive data completionHandler(NSURLSessionResponseAllow); } - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data { // Recevied data NSLog(@"data received---%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]); } - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error { if(!error) { NSLog(@"task completed!"); } else { NSLog(@"%@", error.localizedDescription); } }
- (void)DownloadTaskTest { NSURL *url = [NSURL URLWithString:@"http://pic1.desk.chinaz.com/file/10.03.10/5/rrgaos56_p.jpg"]; NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[[NSOperationQueue alloc] init]]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; // Use download with request or url NSURLSessionDownloadTask *downloadPhotoTask = [session downloadTaskWithRequest:request]; [downloadPhotoTask resume]; } - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location { NSLog(@"set photo"); NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:downloadTask.response.suggestedFilename]; [[NSFileManager defaultManager] moveItemAtURL:location toURL:[NSURL fileURLWithPath:filePath] error:nil]; dispatch_async(dispatch_get_main_queue(), ^{ //Set image in the main thread self.imageView.image = [UIImage imageWithContentsOfFile:filePath]; }); } - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite { // Compute the download progress CGFloat progress = 1.0 * totalBytesWritten / totalBytesExpectedToWrite; NSLog(@"%f",progress); }
// Test GET - (void)httpGetTest { AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; manager.responseSerializer = [AFHTTPResponseSerializer serializer]; // Must add this code, or error occurs // Prepare the URL, parameters can be add to URL (could be html, json, xml, plist address) // NSString *url = @"https:github.com"; NSString *url = @"http://zhan.renren.com/ceshipost"; NSDictionary *params = @{@"tagId":@"18924", @"from":@"template"}; // Params can be nil [manager GET:url parameters:params progress:^(NSProgress * _Nonnull downloadProgress) { // Process the progress here } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { NSLog(@"%@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]); // Can also parse the json file here } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSLog(@"%@", error.localizedDescription); }]; }
// Test POST - (void)httpPostTest { AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; manager.responseSerializer = [AFHTTPResponseSerializer serializer]; // Must add this code, or error occurs // Prepare the URL and parameters NSString *url = @"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx"; NSDictionary *params = [NSMutableDictionary dictionaryWithDictionary:@{@"date":@"20131129", @"startRecord":@"1", @"len":@"5", @"udid":@"1234567890", @"terminalType":@"Iphone", @"cid":@"213"}]; [manager POST:url parameters:params progress:^(NSProgress * _Nonnull uploadProgress) { // Do sth to process progress } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { NSLog(@"%@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]); } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSLog(@"%@", error.localizedDescription); }]; }
// Test Download - (void)downLoadTest { AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; // Set the download URL, could be any format file (jpg,zip,rar,png,mp3,avi.....) NSString *urlString = @"http://img.bizhi.sogou.com/images/1280x1024/2013/07/31/353911.jpg"; NSURL *url = [NSURL URLWithString:urlString]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) { NSLog(@"current downloading progress:%lf", 1.0 * downloadProgress.completedUnitCount / downloadProgress.totalUnitCount); } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) { // The download address NSLog(@"default address%@",targetPath); // Set the real save address instead of the temporary address NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; return [NSURL fileURLWithPath:filePath]; } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) { // When downloaded, do sth NSLog(@"%@ file downloaded to %@", response, [filePath URLByAppendingPathComponent:response.suggestedFilename]); // In the main thread, update UI dispatch_async(dispatch_get_main_queue(), ^{ // Do sth to process UI }); }]; // Launch the download task [downloadTask resume]; }
// Test Upload - (void)upLoadTest { // Can also use configuration initialize manager // NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; // AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration]; AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; // Set the request NSString *urlString = @"http://www.freeimagehosting.net/upload.php"; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]]; [request addValue:@"image/jpeg" forHTTPHeaderField:@"Content-Type"]; [request addValue:@"text/html" forHTTPHeaderField:@"Accept"]; [request setHTTPMethod:@"POST"]; [request setCachePolicy:NSURLRequestReloadIgnoringCacheData]; [request setTimeoutInterval:20]; //Set data to be uploaded UIImage *image = [UIImage imageNamed:@"beauty.jpg"]; NSData *data = UIImageJPEGRepresentation(image, 1.0); // Upload from data or file NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithRequest:request fromData:data progress:^(NSProgress * _Nonnull uploadProgress) { NSLog(@"current uploading progress:%lf", 1.0 * uploadProgress.completedUnitCount / uploadProgress.totalUnitCount); } completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) { // Do sth to response the success NSLog(@"%@", response); }]; // Launch upload [uploadTask resume]; }
// Test the net state watching - (void)watchNetState { AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager]; /* typedef NS_ENUM(NSInteger, AFNetworkReachabilityStatus) { AFNetworkReachabilityStatusUnknown = -1, unkown AFNetworkReachabilityStatusNotReachable = 0, offline AFNetworkReachabilityStatusReachableViaWWAN = 1, cellular AFNetworkReachabilityStatusReachableViaWiFi = 2, wifi }; */ // Once the network changed, this code works [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) { switch(status) { case AFNetworkReachabilityStatusUnknown: NSLog(@"unkown"); break; case AFNetworkReachabilityStatusNotReachable: NSLog(@"offline"); break; case AFNetworkReachabilityStatusReachableViaWWAN: NSLog(@"cellular"); break; case AFNetworkReachabilityStatusReachableViaWiFi: NSLog(@"wifi"); break; default: NSLog(@"????"); break; } }]; // Must start monitoring [manager startMonitoring]; }
// Test set url image - (void)setUrlImage { // Should include a header file, and the image willed cached automatically NSString *imgURL = @"http://img.article.pchome.net/00/44/28/57/pic_lib/wm/7.jpg"; [self.imageView setImageWithURL:[NSURL URLWithString:imgURL] placeholderImage:nil]; }