关于AFNetworking的使用

本文为自己在学习AFNetworking的使用过程中碰到的一些问题,总结成文。

在运行疯狂iOS讲义中关于AFNetworking库的应用时碰到了以下问题。

打开示例代码发现AFURLConnectionOperation.m文件中提示有个_cancelled属性未定义,于是在该文件中添加一句  “@synthesize cancelled = _cancelled;”(注意@synthesize语句需要添加在实现部分——“@implementation”),问题解决。

<span style="font-family:Courier New;font-size:14px;">@implementation AFURLConnectionOperation
@synthesize outputStream = _outputStream;
<span style="color:#ff9966;">@synthesize cancelled = _cancelled;</span>
...</span>

调试AFNetworking应用时,控制台报如下错误:

App Transport Security has blocked acleartext HTTP (http://) resource load since it is insecure. Temporaryexceptions can be configured via your app's Info.plist file.

首先说明一下自己的开发环境(XCode7.3,模拟器采用的 iPhone6s),出现上述错误的原因在于:iOS9.0 由于强制使用https,所以默认不能使用http建立连接。

有两种解决办法:

第一种:使用https协议来连接。

第二种:修改info.plist文件,步骤如下

1.  Info.plist中添加NSAppTransportSecurity类型Dictionary

2.  NSAppTransportSecurity下添加NSAllowsArbitraryLoads类型Boolean,值设为YES

具体请参考 https://segmentfault.com/a/1190000002933776


在文件上传的示例中,点击上传后控制台报告如下错误信息
*** Terminating app due touncaught exception 'NSInvalidArgumentException', reason: '*** -streamStatusonly defined for abstract class.  Define-[AFMultipartBodyStream streamStatus]!'

网络上找到的解决方法:将AFNetWorking更新到最新版本(示例程序中采用的是2.5的版本),但是新版本(3.x)的AFNetworking文件夹中没有AFHTTPRequestOperationManager.hAFHTTPRequestOperationManager.m文件,因此替换为新版本的AFNetworking文件夹会提示找不到AFHTTPRequestOperationManager.h文件。另外,在将AFNetworking文件夹拖入XCode时,选择“Create groups”(如果选择“Create folder references”,编译时也会提示“找不到AFHTTPRequestOperationManager.h文件”)

关于2.x到3.x的迁移,请参考以下链接中的Migration章节:

https://github.com/AFNetworking/AFNetworking/wiki/AFNetworking-3.0-Migration-Guide

AFNetworking Version

Minimum iOS Target

Minimum OS X Target

Notes

3.x

iOS 7

OS X 10.9

Xcode 7+ is required. NSURLConnectionOperation support has been removed.

2.6 -> 2.6.3

iOS 7

OS X 10.9

Xcode 7+ is required.

2.0 -> 2.5.4

iOS 6

OS X 10.8

Xcode 5+ is required. NSURLSession subspec requires iOS 7 or OS X 10.9.


注意:AFNetworking的github页面中,关于版本的说明提到3.x版本中已经不再支持NSURLConnectionOperation。(在XCode7中,Apple官方废除了NSURLConnection的API)如果在之前2.x的版本中使用AFHTTPRequestOperationManager创建连接,那么在3.x的版本中应该使用AFHTTPSessionManager代替。

在AppDelegate.h文件中加入一个AFHTTPSessionManager属性

<span style="font-family:Courier New;font-size:14px;">#import <UIKit/UIKit.h>
#import "AFURLSessionManager.h"
#import "AFHTTPSessionManager.h"

@interface FKAppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) AFHTTPSessionManager* manager;
@end</span>

然后在AppDelegate.m文件的didFinishLaunchingWithOptions:方法中为AFHTTPSessionManager指定一个解析器

<span style="font-family:Courier New;font-size:14px;">- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.manager = [AFHTTPSessionManager manager];
    self.manager.responseSerializer = [[AFHTTPResponseSerializer alloc] init];
    return YES;
}</span>

最后在视图控制器ViewController.m文件中修改按钮对应的方法

<span style="font-family:Courier New;font-size:14px;">    [appDelegate.manager POST:@"http://192.168.1.110:8080/AFNetworkingTest/upload" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
    {
        [formData appendPartWithFileURL:filePath  // 指定上传的文件
                                   name:@"file"  // 指定上传文件对应的请求参数名
         // 指定上传文件的原始文件名
                               fileName:[NSString stringWithFormat:@"%@.png" , fileName]
         // 指定上传文件的MIME类型
                               mimeType:@"image/png"
                                  error:nil];
    }
 success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
     {
         // 当使用HTTP响应解析器时,服务器响应数据被封装在NSData中
         // 此处将NSData转换成NSString、并使用UIAlertView显示登录结果
         [[[UIAlertView alloc] initWithTitle:@"上传结果" message:
           [[NSString alloc] initWithData:responseObject encoding:
            NSUTF8StringEncoding] delegate:self
                           cancelButtonTitle:@"确定" otherButtonTitles:nil]
          show];
     }
 } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
         NSLog(@"获取服务器响应出错!");
     }];</span>





 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值