IOS 网络浅析-(八 NSURLSession简介)

就在不长也不短的时间前,苹果正式命令咱们要向NSURLSession看,因此我们不得不认认真真的听从老大的教导,努力认知NSURLSession。其实呢,三方早已为我们解决了问题,但是呢,我们还是有必要大概了解一下NSURLSession。下面呢,我就为大家简单介绍NSURLSession。

*下面是一位大牛写过的一段话,在此献上*

NSURLConnection在开发中会使用的越来越少,iOS9已经将NSURLConnection废弃,现在最低版本一般适配iOS,所以也可以使用。NSURLConnection上传图片,可以自己找资料。

NSURLConnection相对于NSURLSession,安全性低。NSURLConnection下载有峰值,比较麻烦处理。

尽管适配最低版本iOS7,也可以使用NSURLSession。AFN已经不支持NSURLConnection。

NSURLSession:会话。默认是挂起状态,如果要请求网络,需要开启。

[NSURLSession sharedSession] 获取全局的NSURLSession对象。在iPhone的所有app共用一个全局session.

NSURLSessionUploadTask -> NSURLSessionDataTask -> NSURLSessionTask

NSURLSessionDownloadTask -> NSURLSessionTask

NSURLSessionDownloadTask下载,默认下载到tmp文件夹。下载完成后删除临时文件。所以我们要在删除文件之前,将它移动到Cache里。

下载 测试

//
// ViewController.m
// CX-NSURLSession简介
//
// Created by ma c on 16/3/21.
// Copyright © 2016年 xubaoaichiyu. All rights reserved.
// #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; }
//这是为了测试而建立的点击屏幕事件。
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ //下载 测试 NSURL * url = [NSURL URLWithString:[@"http://localhost/tupian.jpg" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; NSURLRequest * request = [NSURLRequest requestWithURL:url]; NSURLSession * session = [NSURLSession sharedSession]; NSURLSessionDownloadTask * task = [session downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) { NSLog(@"下载完成");
//response.suggestedFilename 响应信息中的资源文件名
NSString * cacheParh = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]stringByAppendingString:response.suggestedFilename];
NSLog(@"缓存地址%@",cacheParh);
//获取文件管理器
NSFileManager * manager = [NSFileManager defaultManager]; //将临时文件移动到缓存目录下
//[NSURL fileURLWithPath:cachesPath] 将本地路径转化为URL类型
//URL如果地址不正确,生成的url对象为空 [manager moveItemAtURL:location toURL:[NSURL fileURLWithPath:cacheParh] error:NULL];
}]; //开启任务
[task resume];
} @end

GET 测试 一

//
// ViewController.m
// CX-NSURLSession简介
//
// Created by ma c on 16/3/21.
// Copyright © 2016年 xubaoaichiyu. All rights reserved.
// #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; }
//这是为了测试而建立的点击屏幕事件。
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ //get测试一 //创建URL
NSURL * url = [NSURL URLWithString:@"http://localhost/login.php?username=haha&password=123"];
//创建 NSURLSession
NSURLSession * session = [NSURLSession sharedSession]; //创建任务
NSURLSessionDataTask * task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { NSLog(@"%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]); }];
//开启任务
[task resume]; } @end

GET 测试 二

//
// ViewController.m
// CX-NSURLSession简介
//
// Created by ma c on 16/3/21.
// Copyright © 2016年 xubaoaichiyu. All rights reserved.
// #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; }
//这是为了测试而建立的点击屏幕事件。
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ //get测试二 //创建URL
NSURL * url = [NSURL URLWithString:@"http://localhost/login.php?username=haha&password=123"];
//创建请求
NSURLRequest * request = [NSURLRequest requestWithURL:url];
//创建 NSURLSession
NSURLSession * session = [NSURLSession sharedSession]; //创建任务 NSURLSessionDataTask * task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { NSLog(@"%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
}];
//开启任务
[task resume]; } @end

POST 测试

//
// ViewController.m
// CX-NSURLSession简介
//
// Created by ma c on 16/3/21.
// Copyright © 2016年 xubaoaichiyu. All rights reserved.
// #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; }
//这是为了测试而建立的点击屏幕事件。
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ //post 测试 //创建URL
NSURL * url = [NSURL URLWithString:@"http://localhost/login.php"];
//创建请求
NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url]; request.HTTPMethod = @"POST"; request.HTTPBody = [@"username=haha&password=123" dataUsingEncoding:NSUTF8StringEncoding]; //创建 NSURLSession
NSURLSession * session = [NSURLSession sharedSession]; //创建任务 NSURLSessionDataTask * task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { NSLog(@"%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
}];
//开启任务
[task resume]; } @end
上一篇:使用ML.NET实现NBA得分预测


下一篇:FFPLAY的原理(四)