NSURLSession 常用方法 get/post 请求 下载
2015-12-03 00:00
274 查看
#import "ViewController.h"
@interface HMViewController () <NSURLSessionDownloadDelegate>
@end
@implementation HMViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
// 任务:任何请求都是一个任务
// NSURLSessionDataTask : 普通的GET\POST请求
// NSURLSessionDownloadTask : 文件下载
// NSURLSessionUploadTask : 文件上传
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self downloadTask2];
}
- (void)downloadTask2
{
NSURLSessionConfiguration *cfg = [NSURLSessionConfiguration defaultSessionConfiguration];
// 1.得到session对象
NSURLSession *session = [NSURLSession sessionWithConfiguration:cfg delegate:self delegateQueue:[NSOperationQueue mainQueue]];
// 2.创建一个下载task
NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/resources/test.mp4"];
NSURLSessionDownloadTask *task = [session downloadTaskWithURL:url];
// 3.开始任务
[task resume];
// 如果给下载任务设置了completionHandler这个block,也实现了下载的代理方法,优先执行block
}
#pragma mark - NSURLSessionDownloadDelegate
/**
* 下载完毕后调用
*
* @param location 临时文件的路径(下载好的文件)
*/
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
{
// location : 临时文件的路径(下载好的文件)
NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
// response.suggestedFilename : 建议使用的文件名,一般跟服务器端的文件名一致
NSString *file = [caches stringByAppendingPathComponent:downloadTask.response.suggestedFilename];
// 将临时文件剪切或者复制Caches文件夹
NSFileManager *mgr = [NSFileManager defaultManager];
// AtPath : 剪切前的文件路径
// ToPath : 剪切后的文件路径
[mgr moveItemAtPath:location.path toPath:file error:nil];
}
/**
* 恢复下载时调用
*/
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes
{
}
/**
* 每当下载完(写完)一部分时就会调用(可能会被调用多次)
*
* @param bytesWritten 这次调用写了多少
* @param totalBytesWritten 累计写了多少长度到沙盒中了
* @param totalBytesExpectedToWrite 文件的总长度
*/
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
{
double progress = (double)totalBytesWritten / totalBytesExpectedToWrite;
NSLog(@"下载进度---%f", progress);
}
#pragma mark ------------------------------------------------------------------
/**
* 下载任务:不能看到下载进度
*/
- (void)downloadTask
{
// 1.得到session对象
NSURLSession *session = [NSURLSession sharedSession];
// 2.创建一个下载task
NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/resources/test.mp4"];
NSURLSessionDownloadTask *task = [session downloadTaskWithURL:url completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
// location : 临时文件的路径(下载好的文件)
NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
// response.suggestedFilename : 建议使用的文件名,一般跟服务器端的文件名一致
NSString *file = [caches stringByAppendingPathComponent:response.suggestedFilename];
// 将临时文件剪切或者复制Caches文件夹
NSFileManager *mgr = [NSFileManager defaultManager];
// AtPath : 剪切前的文件路径
// ToPath : 剪切后的文件路径
[mgr moveItemAtPath:location.path toPath:file error:nil];
}];
// 3.开始任务
[task resume];
}
- (void)dataTask
{
// 1.得到session对象
NSURLSession *session = [NSURLSession sharedSession];
// 2.创建一个task,任务
// NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/video"];
// NSURLSessionDataTask *task = [session dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
// NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
// NSLog(@"----%@", dict);
// }];
NSURL *url = [NSURL URLWithString:@"http://192.168.15.172:8080/MJServer/login"];
// 创建一个请求
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"POST";
// 设置请求体
request.HTTPBody = [@"username=123&pwd=123" dataUsingEncoding:NSUTF8StringEncoding];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
NSLog(@"----%@", dict);
}];
// 3.开始任务
[task resume];
}
@end
@interface HMViewController () <NSURLSessionDownloadDelegate>
@end
@implementation HMViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
// 任务:任何请求都是一个任务
// NSURLSessionDataTask : 普通的GET\POST请求
// NSURLSessionDownloadTask : 文件下载
// NSURLSessionUploadTask : 文件上传
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self downloadTask2];
}
- (void)downloadTask2
{
NSURLSessionConfiguration *cfg = [NSURLSessionConfiguration defaultSessionConfiguration];
// 1.得到session对象
NSURLSession *session = [NSURLSession sessionWithConfiguration:cfg delegate:self delegateQueue:[NSOperationQueue mainQueue]];
// 2.创建一个下载task
NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/resources/test.mp4"];
NSURLSessionDownloadTask *task = [session downloadTaskWithURL:url];
// 3.开始任务
[task resume];
// 如果给下载任务设置了completionHandler这个block,也实现了下载的代理方法,优先执行block
}
#pragma mark - NSURLSessionDownloadDelegate
/**
* 下载完毕后调用
*
* @param location 临时文件的路径(下载好的文件)
*/
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
{
// location : 临时文件的路径(下载好的文件)
NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
// response.suggestedFilename : 建议使用的文件名,一般跟服务器端的文件名一致
NSString *file = [caches stringByAppendingPathComponent:downloadTask.response.suggestedFilename];
// 将临时文件剪切或者复制Caches文件夹
NSFileManager *mgr = [NSFileManager defaultManager];
// AtPath : 剪切前的文件路径
// ToPath : 剪切后的文件路径
[mgr moveItemAtPath:location.path toPath:file error:nil];
}
/**
* 恢复下载时调用
*/
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes
{
}
/**
* 每当下载完(写完)一部分时就会调用(可能会被调用多次)
*
* @param bytesWritten 这次调用写了多少
* @param totalBytesWritten 累计写了多少长度到沙盒中了
* @param totalBytesExpectedToWrite 文件的总长度
*/
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
{
double progress = (double)totalBytesWritten / totalBytesExpectedToWrite;
NSLog(@"下载进度---%f", progress);
}
#pragma mark ------------------------------------------------------------------
/**
* 下载任务:不能看到下载进度
*/
- (void)downloadTask
{
// 1.得到session对象
NSURLSession *session = [NSURLSession sharedSession];
// 2.创建一个下载task
NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/resources/test.mp4"];
NSURLSessionDownloadTask *task = [session downloadTaskWithURL:url completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
// location : 临时文件的路径(下载好的文件)
NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
// response.suggestedFilename : 建议使用的文件名,一般跟服务器端的文件名一致
NSString *file = [caches stringByAppendingPathComponent:response.suggestedFilename];
// 将临时文件剪切或者复制Caches文件夹
NSFileManager *mgr = [NSFileManager defaultManager];
// AtPath : 剪切前的文件路径
// ToPath : 剪切后的文件路径
[mgr moveItemAtPath:location.path toPath:file error:nil];
}];
// 3.开始任务
[task resume];
}
- (void)dataTask
{
// 1.得到session对象
NSURLSession *session = [NSURLSession sharedSession];
// 2.创建一个task,任务
// NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/video"];
// NSURLSessionDataTask *task = [session dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
// NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
// NSLog(@"----%@", dict);
// }];
NSURL *url = [NSURL URLWithString:@"http://192.168.15.172:8080/MJServer/login"];
// 创建一个请求
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"POST";
// 设置请求体
request.HTTPBody = [@"username=123&pwd=123" dataUsingEncoding:NSUTF8StringEncoding];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
NSLog(@"----%@", dict);
}];
// 3.开始任务
[task resume];
}
@end
相关文章推荐
- php5.3升级到5.5
- OpenCV安装
- OpenCV - Mac上使用HighGUI
- OpenCV - "dyld: Library not loaded: cv2.so"
- Netty的UDP多线程源码改造分析
- win8 mysqlzip install
- 设计模式之MVC、KVC、KVO的理解
- android aidl接口初步了解
- oracle dbconsole 因为IP改动启动不了,重建方式
- 《窃听风暴》影评
- 常用正则表达式大全
- Mysql避免全表扫描sql查询优化
- 记一笔
- Mathematics:Ultra-QuickSort(POJ 2299)
- hadoop的体系结构 hadoop hdfs 命令
- java.io.IOException: Bad connect ack with firstBad
- hdfs文件操作操作示例,包括上传文件到HDFS上、从HDFS上下载文件和删除HDFS上的文件
- mysql ORDER BY,GROUP BY 和DISTINCT原理
- 老毛桃u盘安装系统 进入PE进行分区错误
- MySQL utf8mb4 字符集:支持 emoji 表情符号