您的位置:首页 > 移动开发 > IOS开发

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];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息