标签:知识库 更新 nbsp space directory 错误 time 后缀名 后缀
在Xcode7.0之后,苹果废弃了NSURLConnection方法,数据请求使用NSURLSession,作为网络请求类第三方库使用量最大的AFN也及时的更新的新的版本——AFN 3.0版本。新的版本的里废弃了基于NSURLConnection封装的AFHTTPRequestOperationManager,转而使用基于NSURLSession封装的AFHTTPSessionManager了。
下面将详细介绍AFN3.0的使用
1.cocoapods导入AFN 3.0
1.1 进入到工程目录下
$ cd 工程目录名
1.2 搜索最新的AFN库版本
$ pod search AFNetworking
1.3 创建podfile文件
$ vim podfile
1.4 按下键盘“i”键,输入如下的pod语句,然后按下“esc”键,退出编辑,再输入“:wq”,保存退出vim,项目根目录下就会新建我们需要的podfile文件
$ platform:iOS,‘7.0‘
$ pod ‘AFNetworking‘,‘~>3.1.0‘
1.5 导入AFN库,使用如下语句导入和直接$ pod install的区别是略过cocoaPods的库更新过程,导入速度较快
$ pod install --verbose --no-repo-update
导入成功,使用后缀名为.xcworkspace的文件打开工程
2.配置plist文件
在Xcode 7.0之后,苹果使用了https的网路协议,所以在网络请求前,我们还需要配置一下工程的Info.plist文件
右键Info.plist文件 -> open as -> source code ,找到下图位置,粘贴如下代码
- <key>NSAppTransportSecurity</key>
- <dict>
- <key>NSAllowsArbitraryLoads</key>
- <true/>
- </dict>
3.使用AFN进行数据请求
3.1 请求管理类 (HttpsManager)
该类里都是+号方法,不是单例类
在.h里定义超时时间宏,以及成功和失败的block块
- #define kTimeOutInterval 30 // 请求超时的时间
- typedef void (^SuccessBlock)(NSDictionary *dict, BOOL success);
- typedef void (^AFNErrorBlock)(NSError *error);
封装AFN请求管理者
- #pragma mark - 创建请求者
- +(AFHTTPSessionManager *)manager
- {
- AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
-
- manager.requestSerializer.timeoutInterval = kTimeOutInterval;
-
-
- manager.requestSerializer = [AFHTTPRequestSerializer serializer];
-
-
- manager.responseSerializer = [AFHTTPResponseSerializer serializer];
-
- return manager;
- }
3.2 get
- + (void)getUserCarShopAndSalesDataForSalesWithUserId:(NSString *)userId date:(NSString *)date selectAreaType:(NSString *)areaType Success:(SuccessBlock)success fail:(AFNErrorBlock)fail
- {
-
- NSDictionary *param = @{@"user_id":userId, @"sale_date":date, @"accessToken":@"e9c0e60318ebd07ec2fe", @"area_type":areaType};
-
- AFHTTPSessionManager *manager = [self manager];
- [manager GET:@"http://pm.yunhan-china.com/index.php/Api_sale/sales_get" parameters:param progress:^(NSProgress * _Nonnull downloadProgress) {
-
- } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
-
- if(responseObject){
- NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
- success(dict,YES);
- } else {
- success(@{@"msg":@"暂无数据"}, NO);
- }
- } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
-
- fail(error);
- }];
- }
3.3 post
- + (void)loginWithUserAccount:(NSString *)account password:(NSString *)password success:(SuccessBlock)success fail:(AFNErrorBlock)fail
- {
-
- NSDictionary *param = @{@"phoneNumber":account, @"password":@"f379eaf3c831b04de153469d1bec345e"};
-
- AFHTTPSessionManager *manager = [self manager];
- [manager POST:@"http://pm.yunhan-china.com/index.php/api_common/login" parameters:param progress:^(NSProgress * _Nonnull uploadProgress) {
-
- } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
-
- if(responseObject){
- NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
- success(dict,YES);
- } else {
- success(@{@"msg":@"暂无数据"}, NO);
- }
- } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
-
- fail(error);
- }];
-
- }
3.4 下载
- - (void)downLoadWithUrlString:(NSString *)urlString
- {
-
- AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
-
- NSURL *url = [NSURL URLWithString:urlString];
-
- NSURLRequest *request = [NSURLRequest requestWithURL:url];
-
- NSURLSessionDownloadTask *task = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
-
- NSLog(@"当前下载进度为:%lf", 1.0 * downloadProgress.completedUnitCount / downloadProgress.totalUnitCount);
- } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
-
- NSLog(@"默认下载地址%@",targetPath);
-
- NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
- return [NSURL fileURLWithPath:filePath];
- } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
-
- NSLog(@"%@---%@", response, filePath);
- }];
-
- [task resume];
- }
3.5 上传
- - (void)uploadWithUser:(NSString *)userId UrlString:(NSString *)urlString upImg:(UIImage *)upImg
- {
-
- AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
-
- NSDictionary *param = @{@"user_id":userId};
- [manager POST:urlString parameters:param constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
-
-
- NSData *data = UIImagePNGRepresentation(upImg);
-
- [formData appendPartWithFileData:data name:@"file" fileName:@"123.png" mimeType:@"image/png"];
-
-
- } progress:^(NSProgress * _Nonnull uploadProgress) {
-
- NSLog(@"%lf",1.0 *uploadProgress.completedUnitCount / uploadProgress.totalUnitCount);
- } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
-
- NSLog(@"请求成功:%@",responseObject);
-
- } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
-
- NSLog(@"请求失败:%@",error);
- }];
- }
- 3.6 网络监听
- - (void)AFNetworkStatus{
-
-
- AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
-
-
-
- [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
-
-
- switch (status) {
- case AFNetworkReachabilityStatusUnknown:
- NSLog(@"未知网络状态");
- break;
- case AFNetworkReachabilityStatusNotReachable:
- NSLog(@"无网络");
- break;
-
- case AFNetworkReachabilityStatusReachableViaWWAN:
- NSLog(@"蜂窝数据网");
- break;
-
- case AFNetworkReachabilityStatusReachableViaWiFi:
- NSLog(@"WiFi网络");
-
- break;
-
- default:
- break;
- }
-
- }] ;
- }
iOS AFNetworking 3.0以上使用
标签:知识库 更新 nbsp space directory 错误 time 后缀名 后缀
原文地址:http://www.cnblogs.com/caodedi-88/p/6292372.html