iOS中NSURLSession的使用
2015-10-28 23:48
274 查看
转自http://m.blog.csdn.net/blog/lsc19890723/40953983
快一年左右未在CSDN上发表新文章了,基本上都保存在自己的本地。难得闲下来,还是将自己的一些资料发布到网上供大家一起交流,参考。
NSURLSession是苹果一种HTTP网络请求方式,想必有经验的都知道在iOS开发中,HTTP网络请求方式有很多种。比如:
ASIHTTPRequest
AFNetworking等
在以前的开发中,我比较习惯使用ASIHTTPRequest,但是自从ARC之后,ASIHTTPRequest没有更新升级了,不过还是能够满足日常的开发需求,同时使用cocospods管理,ASIHTTPRequest在ARC的条件下,也比较方便了。当然ASIHTTPRequest与AFNetworking之间各有优缺点。同时毕竟是第三方库存在太大不确定的因素,为此,我在日常的开发当中,慢慢的移除第三方库或自己基于苹果提供的API进行二次封装,提供接口。好了,不废话了,切入本文的主题,NSURLSession的使用,直接上代码:
1、简单请求
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.baidu.com"]];
NSURLSession *urlSession = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [urlSession dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{
NSLog(@"data > %@", data);
}];
[dataTask resume];
// [dataTask suspend];
// [dataTask cancel];
NSURLSessionDownloadTask *downloadTask = [urlSession downloadTaskWithRequest:request
completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error)
{
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSURL *documentsDirectoryURL = [NSURL fileURLWithPath:documentsPath];
4000
NSURL *newFileLocation = [documentsDirectoryURL URLByAppendingPathComponent:[[response URL] lastPathComponent]];
[[NSFileManager defaultManager] copyItemAtURL:location toURL:newFileLocation error:nil];
}];
[downloadTask resume];
NSURLSessionUploadTask *uploadTask = [urlSession uploadTaskWithRequest:request
fromData:nil
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{
}];
[uploadTask resume];
2、POST请求
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"url地址"]];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[@"version=1.0.0&os_version=7.1.2&os_type=iOS" dataUsingEncoding:NSUTF8StringEncoding]];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{
NSLog(@"获取到的数据>%@",
[[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding]);
}];
[dataTask resume];
快一年左右未在CSDN上发表新文章了,基本上都保存在自己的本地。难得闲下来,还是将自己的一些资料发布到网上供大家一起交流,参考。
NSURLSession是苹果一种HTTP网络请求方式,想必有经验的都知道在iOS开发中,HTTP网络请求方式有很多种。比如:
ASIHTTPRequest
AFNetworking等
在以前的开发中,我比较习惯使用ASIHTTPRequest,但是自从ARC之后,ASIHTTPRequest没有更新升级了,不过还是能够满足日常的开发需求,同时使用cocospods管理,ASIHTTPRequest在ARC的条件下,也比较方便了。当然ASIHTTPRequest与AFNetworking之间各有优缺点。同时毕竟是第三方库存在太大不确定的因素,为此,我在日常的开发当中,慢慢的移除第三方库或自己基于苹果提供的API进行二次封装,提供接口。好了,不废话了,切入本文的主题,NSURLSession的使用,直接上代码:
1、简单请求
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.baidu.com"]];
NSURLSession *urlSession = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [urlSession dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{
NSLog(@"data > %@", data);
}];
[dataTask resume];
// [dataTask suspend];
// [dataTask cancel];
NSURLSessionDownloadTask *downloadTask = [urlSession downloadTaskWithRequest:request
completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error)
{
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSURL *documentsDirectoryURL = [NSURL fileURLWithPath:documentsPath];
4000
NSURL *newFileLocation = [documentsDirectoryURL URLByAppendingPathComponent:[[response URL] lastPathComponent]];
[[NSFileManager defaultManager] copyItemAtURL:location toURL:newFileLocation error:nil];
}];
[downloadTask resume];
NSURLSessionUploadTask *uploadTask = [urlSession uploadTaskWithRequest:request
fromData:nil
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{
}];
[uploadTask resume];
2、POST请求
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"url地址"]];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[@"version=1.0.0&os_version=7.1.2&os_type=iOS" dataUsingEncoding:NSUTF8StringEncoding]];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{
NSLog(@"获取到的数据>%@",
[[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding]);
}];
[dataTask resume];
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 讲解iOS开发中基本的定位功能实现
- js判断客户端是iOS还是Android等移动终端的方法
- IOS开发环境windows化攻略
- 检测iOS设备是否越狱的方法
- .net平台推送ios消息的实现方法
- 探讨Android与iOS,我们将何去何从?
- Android网络请求库android-async-http介绍
- Android、iOS和Windows Phone中的推送技术详解
- IOS 改变键盘颜色代码
- 举例详解iOS开发过程中的沙盒机制与文件
- Android和IOS的浏览器中检测是否安装某个客户端的方法
- javascript实现阻止iOS APP中的链接打开Safari浏览器
- IOS开发第三方语音-微信语音