AFNetworking慢慢整理
来源:互联网 发布:笔记 软件 编辑:程序博客网 时间:2024/06/06 04:18
头文件
#import "AFURLRequestSerialization.h" #import "AFURLResponseSerialization.h" #import "AFSecurityPolicy.h" #import "AFNetworkReachabilityManager.h" #import "AFURLSessionManager.h" #import "AFHTTPSessionManager.h"
AFURLSessionManager
- (void)dataTask{ NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration]; NSURL *URL = [NSURL URLWithString:@"http://httpbin.org/get"]; NSURLRequest *request = [NSURLRequest requestWithURL:URL]; NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) { if (error) { NSLog(@"Error: %@", error); } else { NSLog(@"%@ %@", response, responseObject); } }]; [dataTask resume];}
- (void)uploadTask{ NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration]; NSURL *URL = [NSURL URLWithString:@"http://example.com/upload"]; NSURLRequest *request = [NSURLRequest requestWithURL:URL]; NSURL *filePath = [NSURL fileURLWithPath:@"file://path/to/image.png"]; NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithRequest:request fromFile:filePath progress:nil completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) { if (error) { NSLog(@"Error: %@", error); } else { NSLog(@"Success: %@ %@", response, responseObject); } }]; [uploadTask resume];}
- (void)downloadTask{ NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration]; NSURL *URL = [NSURL URLWithString:@"http://example.com/download.zip"]; NSURLRequest *request = [NSURLRequest requestWithURL:URL]; NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) { NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil]; return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]]; } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) { NSLog(@"File downloaded to: %@", filePath); }]; [downloadTask resume];}
AFHTTPSessionManager
继承AFURLSessionManager
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; [manager GET:@"http://httpbin.org/get" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { NSLog(@"%@", responseObject); } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSLog(@"Error: %@", error); }];
AFNetworkReachabilityManager.h
判断当前是什么网络状态
[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) { NSLog(@"Reachability: %@", AFStringFromNetworkReachabilityStatus(status)); }]; [[AFNetworkReachabilityManager sharedManager] startMonitoring];
0 0
- AFNetworking慢慢整理
- GCD慢慢整理
- NSOperation慢慢整理
- NSInvocation慢慢整理
- git慢慢整理
- ios事件慢慢整理
- URLSession慢慢整理
- 常用接口整理(慢慢补充)
- 正则表达式(慢慢整理)
- IOS9新特性慢慢整理
- 有关AFNetworking资料的整理
- 位操作大全(慢慢翻译整理)
- 通信中的基本概念(慢慢整理)
- 值得收藏的网站,慢慢整理.
- Bit Twiddling Hacks(好东西慢慢整理)
- Android学习之路---慢慢整理中......
- 计算几何模板(慢慢整理中~)
- 计算机网络基础知识整理(慢慢补全。)
- ZooKeeper系列之十:ZooKeeper的一致性保证及Leader选举
- Android中Monkey测试的使用
- Git 常用命令行
- Redis关键点(自动bgrewriteaof)
- 内存管理三之函数篇
- AFNetworking慢慢整理
- 为对象的属性设置属性值
- 栈帧
- zend studio html中快捷书写<?php ?>标签并且改变颜色:
- iOS日期前后比较 && 日期处理
- 动效设计入门工具小手册(微交互)
- leak-canary
- Delphi : Opendialog 文件过滤类型,文件多选, 使用多文件
- hibernate缓存机制详细分析(一级/二级/查询缓存)