NSURLConnection和RunLoop
默认情况会将NSURLConnection添加当前线程到RunLoop,如果是在子线程中调用NSURLConnection可能会有问题, 因为子线程默认没有RunLoop
如何让代理方法在子线程中执行?
- [conn setDelegateQueue:[[NSOperationQueue alloc] init]];
注意:
- NSURLConnection是异步请求
// 1.发送请求
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://120.25.226.186:32812/resources/images/minion_01.png"]] ;
NSRunLoop *runloop = [NSRunLoop currentRunloop];
NSURLConnection *conn = [NSURLConnection connectionWithRequest:request delegate:self];
[conn setDelegateQueue:[[NSOperationQueue alloc] init]];
// 2.启动runLoop
// 默认情况下子线程没有RunLoop, 所以需要启动
[runloop run];
// 3.如果手动发送请求, 系统内部会自动帮子线程创建一个RunLoop
// NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
// [conn start];
NSURLSession
- 使用步骤
- 创建NSURLSession
- 创建Task
- 执行Task
NSURLSessionDataTask
- GET
NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/login?username=520it&pwd=520it"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 1.获得NSURLSession对象
NSURLSession *session = [NSURLSession sharedSession];
// 2.创建任务
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(@"%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
}];
// 3.启动任务
[task resume];
NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/login?username=520it&pwd=520it"];
// 1.获得NSURLSession对象
NSURLSession *session = [NSURLSession sharedSession];
// 2.创建任务
NSURLSessionDataTask *task = [session dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(@"%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
}];
// 3.启动任务
[task resume];
- POST
NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/login"];
// 设置请求头
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"POST";
request.HTTPBody = [@"username=520it&pwd=520it" dataUsingEncoding:NSUTF8StringEncoding];
// 1.获得NSURLSession对象
NSURLSession *session = [NSURLSession sharedSession];
// 2.创建任务
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(@"%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
}];
// 3.启动任务
[task resume];
- NSURLSessionDataDelegate代理注意事项
- Task都是通过Session监听
// 1.获得NSURLSession对象
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[[NSOperationQueue alloc] init]];
+ NSURLSessionDataDelegate`默认不会接受数据`
/**
* 1.接收到服务器的响应
*/
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler
{
NSLog(@"%s", __func__);
NSLog(@"%@", [NSThread currentThread]);
/*
NSURLSessionResponseAllow: 去向处理服务器响应, 内部会调用[task cancle]
NSURLSessionResponseCancel: 允许处理服务器的响应,才会继续接收服务器返回的数据
NSURLSessionResponseBecomeDownload: 讲解当前请求变为下载
*/
completionHandler(NSURLSessionResponseAllow);
}
/**
* 2.接收到服务器的数据(可能会被调用多次)
*/
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data
{
NSLog(@"%s", __func__);
}
/**
* 3.请求成功或者失败(如果失败,error有值)
*/
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
NSLog(@"%s", __func__);
}
NSURLSessionDownloadTask
- DownloadTask默认已经实现边下载边写入
// 1.创建request
NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/resources/videos/minion_01.mp4"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 2.创建session
NSURLSession *session = [NSURLSession sharedSession];
// 3.创建下载任务
NSURLSessionDownloadTask *task = [session downloadTaskWithRequest:request completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
NSLog(@"%@", location.absoluteString);
NSFileManager *manager = [NSFileManager defaultManager];
NSString *toPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
toPath = [toPath stringByAppendingPathComponent:response.suggestedFilename];
// 将下载好的文件从location移动到cache
[manager moveItemAtURL:location toURL:[NSURL fileURLWithPath:toPath] error:nil];
}];
// 4.开启下载任务
[task resume];
- DownloadTask断点下载
- 暂停: suspend
- 继续: resume
- 取消: cancel
- 任务一旦被取消就无法恢复
- 区别并返回下载信息
//取消并获取当前下载信息
[self.task cancelByProducingResumeData:^(NSData *resumeData) {
self.resumeData = resumeData;
}];
// 根据用户上次的下载信息重新创建任务
self.task = [self.session downloadTaskWithResumeData:self.resumeData];
[self.task resume];
- DownloadTask监听下载进度
/**
* 每当写入数据到临时文件时,就会调用一次这个方法
* totalBytesExpectedToWrite:总大小
* totalBytesWritten: 已经写入的大小
* bytesWritten: 这次写入多少
*/
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
{
NSLog(@"正在下载: %.2f", 1.0 * totalBytesWritten / totalBytesExpectedToWrite);
}
/*
* 根据resumeData恢复任务时调用
* expectedTotalBytes:总大小
* fileOffset: 已经写入的大小
*/
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes
{
NSLog(@"didResumeAtOffset fileOffset = %lld , expectedTotalBytes = %lld", fileOffset, expectedTotalBytes);
}
/**
* 下载完毕就会调用一次这个方法
*/
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
{
NSLog(@"下载完毕");
// 文件将来存放的真实路径
NSString *file = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:downloadTask.response.suggestedFilename];
// 剪切location的临时文件到真实路径
NSFileManager *mgr = [NSFileManager defaultManager];
[mgr moveItemAtURL:location toURL:[NSURL fileURLWithPath:file] error:nil];
}
/**
* 任务完成
*/
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
NSLog(@"didCompleteWithError");
}
离线断点下载
- 使用NSURLSessionDataTask
NSMutableURLRequest *reuqest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://120.25.226.186:32812/resources/videos/minion_01.mp4"]];
// 核心方法, 实现从指定位置开始下载
NSInteger currentBytes = KCurrentBytes;
NSString *range = [NSString stringWithFormat:@"bytes=%zd-", currentBytes];
[reuqest setValue:range forHTTPHeaderField:@"Range"];
// 核心方法, 自己实现边下载边写入
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data;
NSURLSessionUploadTask
- 设置设置请求头, 告诉服务器是文件上传
- multipart/form-data; boundary=lnj
- 必须自己
严格按照格式
拼接请求体 - 请求体不能设置给request, 只能设置给fromData
- The body stream and body data in this request object are ignored.
+
- The body stream and body data in this request object are ignored.
[[self.session uploadTaskWithRequest:request fromData:body completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(@"%@", [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]);
}] resume];
- 注意:以下方法用于PUT请求
[self.session uploadTaskWithRequest:<#(nonnull NSURLRequest *)#> fromFile:<#(nonnull NSURL *)#>]
- 监听上传进度
/*
bytesSent: 当前发送的文件大小
totalBytesSent: 已经发送的文件总大小
totalBytesExpectedToSend: 需要发送的文件大小
*/
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData:(int64_t)bytesSent totalBytesSent:(int64_t)totalBytesSent totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend
{
NSLog(@"%zd, %zd, %zd", bytesSent, totalBytesSent, totalBytesExpectedToSend);
}
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
NSLog(@"%s", __func__);
}
AFN
NSURLConnection包装方法
GET
// 1.创建AFN管理者 // AFHTTPRequestOperationManager内部包装了NSURLConnection AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; // 2.利用AFN管理者发送请求 NSDictionary *params = @{ @"username" : @"520it", @"pwd" : @"520it" }; [manager GET:@"http://120.25.226.186:32812/login" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"请求成功---%@", responseObject); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"请求失败---%@", error); }];
POST
// 1.创建AFN管理者 // AFHTTPRequestOperationManager内部包装了NSURLConnection AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; // 2.利用AFN管理者发送请求 NSDictionary *params = @{ @"username" : @"520it", @"pwd" : @"520it" }; [manager POST:@"http://120.25.226.186:32812/login" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"请求成功---%@", responseObject); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"请求失败---%@", error); }];
NSURLSession包装方法
GET
// 1.创建AFN管理者 AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; // 2.利用AFN管理者发送请求 NSDictionary *params = @{ @"username" : @"520it", @"pwd" : @"520it" }; [manager GET:@"http://120.25.226.186:32812/login" parameters:params success:^(NSURLSessionDataTask *task, id responseObject) { NSLog(@"请求成功---%@", responseObject); } failure:^(NSURLSessionDataTask *task, NSError *error) { NSLog(@"请求失败---%@", error); }];
POST
// 1.创建AFN管理者 AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; // 2.利用AFN管理者发送请求 NSDictionary *params = @{ @"username" : @"520it", @"pwd" : @"520it" }; [manager POST:@"http://120.25.226.186:32812/login" parameters:params success:^(NSURLSessionDataTask *task, id responseObject) { NSLog(@"请求成功---%@", responseObject); } failure:^(NSURLSessionDataTask *task, NSError *error) { NSLog(@"请求失败---%@", error); }];
文件下载
// 1.创建AFN管理者
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
// 2.利用AFN管理者发送请求
NSURLRequest *reuqest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://120.25.226.186:32812/resources/videos/minion_02.mp4"]];
[[manager downloadTaskWithRequest:reuqest progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
// targetPath: 已经下载好的文件路径
NSLog(@"targetPath = %@", targetPath);
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSURL *documentsDirectoryPath = [NSURL fileURLWithPath:[path stringByAppendingPathComponent:response.suggestedFilename]];
// 返回需要保存文件的目标路径
return documentsDirectoryPath;
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
NSLog(@"filePath = %@", filePath);
}] resume];
- 监听进度
/*
要跟踪进度,需要使用 NSProgress,是在 iOS 7.0 推出的,专门用来跟踪进度的类!
NSProgress只是一个对象!如何跟踪进度!-> KVO 对属性变化的监听!
@property int64_t totalUnitCount; 总单位数
@property int64_t completedUnitCount; 完成单位数
*/
NSProgress *progress = nil;
// 注册通知
[progress addObserver:self forKeyPath:@"completedUnitCount" options:NSKeyValueObservingOptionNew context:nil];
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
NSLog(@"%@", object);
/**
准确的获得进度
localizedDescription 10%
localizedAdditionalDescription completed 32,768 of 318,829
fractionCompleted 0.102776(completedUnitCount/totalUnitCount)
*/
if ([object isKindOfClass:[NSProgress class]]) {
NSProgress *p = (NSProgress *)object;
NSLog(@"%@, %@, %f", p.localizedDescription, p.localizedAdditionalDescription, p.fractionCompleted);
- 文件上传
// 1.创建AFN管理者
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
// 2.利用AFN管理者发送请求
[manager POST:@"http://120.25.226.186:32812/upload" parameters:@{@"username" : @"lnj"} constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
NSData *data = [NSData dataWithContentsOfFile:@"/Users/NJ-Lee/Desktop/Snip20150811_1.png"];
[formData appendPartWithFileData:data name:@"file" fileName:@"lnj.png" mimeType:@"image/png"];
} success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(@"请求成功---%@", responseObject);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"请求失败---%@", error);
}];
[formData appendPartWithFileURL:[NSURL fileURLWithPath:@"/Users/NJ-Lee/Desktop/Snip20150811_1.png"] name:@"file" fileName:@"lnj.png" mimeType:@"image/png" error:nil];
[formData appendPartWithFileURL:[NSURL fileURLWithPath:@"/Users/NJ-Lee/Desktop/Snip20150811_1.png"] name:@"file" error:nil];
AFN解耦
- 自定义单利类基础Manager
- 优点: 替换框架只需要求改单利类即可
序列化
- AFN默认将服务器返回的数据当做JSON处理, 会自动解析
- manager.responseSerializer = [AFJSONRequestSerializer serializer];
- 告诉AFN,以XML形式解析服务器返回的数据
- manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
- 告诉AFN, 不处理服务器返回的数据, 原样返回
- manager.responseSerializer = [AFHTTPResponseSerializer serializer];
- AFN默认将服务器返回的数据当做JSON处理, 会自动解析
网络状态检测
- AFN
// 1.创建网络监听对象
AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
// 2.设置网络状态改变回调
[manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
/*
AFNetworkReachabilityStatusUnknown = -1, // 未知
AFNetworkReachabilityStatusNotReachable = 0, // 无连接
AFNetworkReachabilityStatusReachableViaWWAN = 1, // 3G 花钱
AFNetworkReachabilityStatusReachableViaWiFi = 2, // 局域网络,不花钱
*/
switch (status) {
case 0:
NSLog(@"无连接");
break;
case 1:
NSLog(@"3G 花钱");
break;
case 2:
NSLog(@"局域网络,不花钱");
break;
default:
NSLog(@"未知");
break;
}
}];
// 3.开始监听
[manager startMonitoring];
- 苹果自带
“`objc
- (void)viewDidLoad {
[super viewDidLoad];
// 注册通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getNetworkStatus) name:kReachabilityChangedNotification object:nil];
// 开始监听网络
self.reachability = [Reachability reachabilityForInternetConnection];
[self.reachability startNotifier];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[self.reachability stopNotifier];
} - (void)getNetworkStatus{
if ([Reachability reachabilityForLocalWiFi].currentReachabilityStatus != NotReachable) {
NSLog(@”wifi”);
}else if ([Reachability reachabilityForInternetConnection].currentReachabilityStatus != NotReachable)
{
NSLog(@”手机自带网络”);
}else
{
NSLog(@”没有网络”);
}
}