美文网首页网络通信iOS技术点ios开发学习系列
基于AFNetworking 3.0的集约性网络请求API再封装

基于AFNetworking 3.0的集约性网络请求API再封装

作者: CharlesAir | 来源:发表于2016-05-24 23:43 被阅读4020次
i fought,but i lost,then i rest

MyZone


关于AFNetworking 3.0

NSURLConnection的API已废弃


AFNetworking 1.0 是建立在 NSURLConnection基础上的,AFNetworking 2.0 开始使用基于 NSURLConnection API基础功能,同时也有基于新的NSURLSession API 的功能实现。AFNetworking 3.0现在是专门建立在 NSURLSession 顶层的,这降低了维护负担,同时允许支持苹果为 NSURLSession 提供的任何额外的增强的特性。在Xcode 7NSURLConnection API 已经被苹果官方弃用。然而API函数将继续使用不会受影响,只不过再也不会添加新的功能了,苹果建议所有基于网络的功能在未来都能使用 NSURLSession


3.0 被移除的类有

• AFURLConnectionOperation
• AFHTTPRequestOperation
• AFHTTPRequestOperationManager


转而替代的是

• AFURLSessionManager
• AFHTTPSessionManager

下面的方法也是基于NSURLSession进行封装的
所有的请求方法都在NetWorkManager.m进行统一设置,并且全部为类方法(调用方便)


tips

1.设置网路请求的BaseURL,请移步Supporting Files-->const.h中进行修改

    #define BaseURL @"http://apis.baidu.com/apistore"

   //请正确设置baseUrl的格式 否则会报无效的url(可参考demo中的设置)

2.关于tokken的设置


 #warning 此处做为测试 可根据自己应用设置相应的值 
 /**设置apikey ------类似于自己应用中的tokken---此处仅仅作为测试使用*/
 [self.requestSerializer setValue:apikey forHTTPHeaderField:@"apikey"];


主要实现的功能

1.文件下载
2.多图压缩上传
3.视频上传(文件上传,音频上传类似)
4.取消所有的网络请求
5.取消指定的网络请求
暂未考虑缓存请求到的数据


基本的post,get请求

/**
 *  网络请求的实例方法
 *
 *  @param type         get / post
 *  @param urlString    请求的地址
 *  @param paraments    请求的参数
 *  @param successBlock 请求成功的回调
 *  @param failureBlock 请求失败的回调
 *  @param progress 进度
 */
+(void)requestWithType:(HttpRequestType)type withUrlString:(NSString *)urlString withParaments:(id)paraments withSuccessBlock:( requestSuccess)successBlock withFailureBlock:( requestFailure)failureBlock progress:(downloadProgress)progress;
   

基本的post,get请求实现

/**
 *  网络请求的实例方法
 *
 *  @param type         get / post
 *  @param urlString    请求的地址
 *  @param paraments    请求的参数
 *  @param successBlock 请求成功的回调
 *  @param failureBlock 请求失败的回调
 *  @param progress 进度
 */

+(void)requestWithType:(HttpRequestType)type withUrlString:(NSString *)urlString withParaments:(id)paraments withSuccessBlock:(requestSuccess)successBlock withFailureBlock:(requestFailure)failureBlock progress:(downloadProgress)progress
{
    
    
    switch (type) {
            
        case HttpRequestTypeGet:
        {
            
            
            [[NetWorkManager shareManager] GET:urlString parameters:paraments progress:^(NSProgress * _Nonnull downloadProgress) {
                
                progress(downloadProgress.completedUnitCount / downloadProgress.totalUnitCount);
                
            } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                
                      successBlock(responseObject);
                
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                
                       failureBlock(error);
            }];
            
            break;
        }
            
        case HttpRequestTypePost:
            
        {
            
            [[NetWorkManager shareManager] POST:urlString parameters:paraments progress:^(NSProgress * _Nonnull uploadProgress) {
                
                progress(uploadProgress.completedUnitCount / uploadProgress.totalUnitCount);

                
            } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                
                    successBlock(responseObject);
                
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                
                     failureBlock(error);
                
            }];
        }
            
    }
    
}

上传图片

/**
 *  上传图片
 *
 *  @param operations   上传图片预留参数---视具体情况而定 可移除
 *  @param imageArray   上传的图片数组
 *  @parm width      图片要被压缩到的宽度
 *  @param urlString    上传的url
 *  @param successBlock 上传成功的回调
 *  @param failureBlock 上传失败的回调
 *  @param progress     上传进度
 */

+(void)uploadImageWithOperations:(NSDictionary *)operations withImageArray:(NSArray *)imageArray withtargetWidth:(CGFloat )width withUrlString:(NSString *)urlString withSuccessBlock:(requestSuccess)successBlock withFailurBlock:(requestFailure)failureBlock withUpLoadProgress:(uploadProgress)progress;


上传图片实现

/**
 *  上传图片
 *
 *  @param operations   上传图片等预留参数---视具体情况而定 可移除
 *  @param imageArray   上传的图片数组
 *  @parm width      图片要被压缩到的宽度
 *  @param urlString    上传的url---请填写完整的url
 *  @param successBlock 上传成功的回调
 *  @param failureBlock 上传失败的回调
 *  @param progress     上传进度
 *
 */
+(void)uploadImageWithOperations:(NSDictionary *)operations withImageArray:(NSArray *)imageArray withtargetWidth:(CGFloat )width withUrlString:(NSString *)urlString withSuccessBlock:(requestSuccess)successBlock withFailurBlock:(requestFailure)failureBlock withUpLoadProgress:(uploadProgress)progress;
{
    //1.创建管理者对象
      AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
     [manager POST:urlString parameters:operations constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
        NSUInteger i = 0 ;
        
        /**出于性能考虑,将上传图片进行压缩*/
        for (UIImage * image in imageArray) {
            
            //image的分类方法
            UIImage *  resizedImage =  [UIImage IMGCompressed:image targetWidth:width];
            
            NSData * imgData = UIImageJPEGRepresentation(resizedImage, .5);
            
            //拼接data
            [formData appendPartWithFileData:imgData name:[NSString stringWithFormat:@"picflie%ld",(long)i] fileName:@"image.png" mimeType:@" image/jpeg"];
            
            i++;
        }
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
        progress(uploadProgress.completedUnitCount / uploadProgress.totalUnitCount);
        
    } success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
        
        successBlock(responseObject);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
       
        failureBlock(error);
        
    }];
}

视频上传

/**
 *  视频上传
 *
 *  @param operations   上传视频预留参数---视具体情况而定 可移除
 *  @param videoPath    上传视频的本地沙河路径
 *  @param urlString     上传的url
 *  @param successBlock 成功的回调
 *  @param failureBlock 失败的回调
 *  @param progress     上传的进度
 */
+(void)uploadVideoWithOperaitons:(NSDictionary *)operations withVideoPath:(NSString *)videoPath withUrlString:(NSString *)urlString withSuccessBlock:(requestSuccess)successBlock withFailureBlock:(requestFailure)failureBlock withUploadProgress:(uploadProgress)progress;

视频上传实现

/**
 *  视频上传
 *
 *  @param operations   上传视频预留参数---视具体情况而定 可移除
 *  @param videoPath    上传视频的本地沙河路径
 *  @param urlString     上传的url
 *  @param successBlock 成功的回调
 *  @param failureBlock 失败的回调
 *  @param progress     上传的进度
 */

+(void)uploadVideoWithOperaitons:(NSDictionary *)operations withVideoPath:(NSString *)videoPath withUrlString:(NSString *)urlString withSuccessBlock:(requestSuccess)successBlock withFailureBlock:(requestFailure)failureBlock withUploadProgress:(uploadProgress)progress
{
    
    
    /**获得视频资源*/
    
    AVURLAsset * avAsset = [AVURLAsset assetWithURL:[NSURL URLWithString:videoPath]];

    /**压缩*/
    
//    NSString *const AVAssetExportPreset640x480;
//    NSString *const AVAssetExportPreset960x540;
//    NSString *const AVAssetExportPreset1280x720;
//    NSString *const AVAssetExportPreset1920x1080;
//    NSString *const AVAssetExportPreset3840x2160;
    
    AVAssetExportSession  *  avAssetExport = [[AVAssetExportSession alloc] initWithAsset:avAsset presetName:AVAssetExportPreset640x480];
    
    /**创建日期格式化器*/
    
    NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
    
    [formatter setDateFormat:@"yyyy-MM-dd-HH:mm:ss"];
    
    /**转化后直接写入Library---caches*/
    
    NSString *  videoWritePath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingString:[NSString stringWithFormat:@"/output-%@.mp4",[formatter stringFromDate:[NSDate date]]]];
    
    
    avAssetExport.outputURL = [NSURL URLWithString:videoWritePath];
    
    
    avAssetExport.outputFileType =  AVFileTypeMPEG4;
    
    
    [avAssetExport exportAsynchronouslyWithCompletionHandler:^{
       
        
        switch ([avAssetExport status]) {
                
                
            case AVAssetExportSessionStatusCompleted:
            {
                
                
                
                AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
                
                [manager POST:urlString parameters:operations constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
                    
                    //获得沙盒中的视频内容
                    
                    [formData appendPartWithFileURL:[NSURL fileURLWithPath:videoWritePath] name:@"write you want to writre" fileName:videoWritePath mimeType:@"video/mpeg4" error:nil];
                    
                } progress:^(NSProgress * _Nonnull uploadProgress) {
                    
                       progress(uploadProgress.completedUnitCount / uploadProgress.totalUnitCount);
                    
                } success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
                    
                    successBlock(responseObject);
                    
                } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                    
                    failureBlock(error);
                    
                }];
                
                break;
            }
            default:
                break;
        }
        
        
    }];

}

文件下载

#pragma mark - 文件下载


/**
 *  文件下载
 *
 *  @param operations   文件下载预留参数---视具体情况而定 可移除
 *  @param savePath     下载文件保存路径
 *  @param urlString        请求的url
 *  @param successBlock 下载文件成功的回调
 *  @param failureBlock 下载文件失败的回调
 *  @param progress     下载文件的进度显示
 */

+(void)downLoadFileWithOperations:(NSDictionary *)operations withSavaPath:(NSString *)savePath withUrlString:(NSString *)urlString withSuccessBlock:(requestSuccess)successBlock withFailureBlock:(requestFailure)failureBlock withDownLoadProgress:(downloadProgress)progress

文件下载实现


/**
 *  文件下载
 *
 *  @param operations   文件下载预留参数---视具体情况而定 可移除
 *  @param savePath     下载文件保存路径
 *  @param urlString        请求的url
 *  @param successBlock 下载文件成功的回调
 *  @param failureBlock 下载文件失败的回调
 *  @param progress     下载文件的进度显示
 */

+(void)downLoadFileWithOperations:(NSDictionary *)operations withSavaPath:(NSString *)savePath withUrlString:(NSString *)urlString withSuccessBlock:(requestSuccess)successBlock withFailureBlock:(requestFailure)failureBlock withDownLoadProgress:(downloadProgress)progress
{
    
    
    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
    
    
    [manager downloadTaskWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]] progress:^(NSProgress * _Nonnull downloadProgress) {
        
        progress(downloadProgress.completedUnitCount / downloadProgress.totalUnitCount);

        
    } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
        
                return  [NSURL URLWithString:savePath];
        
    } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
        
        if (error) {
            
            failureBlock(error);
        }
        
    }];
    
}

取消指定的url请求

/**
 *  取消指定的url请求
 *
 *  @param requestType 该请求的请求类型
 *  @param string      该请求的url
 */

+(void)cancelHttpRequestWithRequestType:(NSString *)requestType requestUrlString:(NSString *)string;

取消指定的url请求实现

/**
 *  取消指定的url请求
 *
 *  @param requestType 该请求的请求类型
 *  @param string      该请求的完整url
 */

+(void)cancelHttpRequestWithRequestType:(NSString *)requestType requestUrlString:(NSString *)string
{
    
    NSError * error;
    
    /**根据请求的类型 以及 请求的url创建一个NSMutableURLRequest---通过该url去匹配请求队列中是否有该url,如果有的话 那么就取消该请求*/
    
    NSString * urlToPeCanced = [[[[NetWorkManager shareManager].requestSerializer requestWithMethod:requestType URLString:string parameters:nil error:&error] URL] path];
    
    
    for (NSOperation * operation in [NetWorkManager shareManager].operationQueue.operations) {
        
        //如果是请求队列
        if ([operation isKindOfClass:[NSURLSessionTask class]]) {
            
            //请求的类型匹配
            BOOL hasMatchRequestType = [requestType isEqualToString:[[(NSURLSessionTask *)operation currentRequest] HTTPMethod]];
            
            //请求的url匹配
            
            BOOL hasMatchRequestUrlString = [urlToPeCanced isEqualToString:[[[(NSURLSessionTask *)operation currentRequest] URL] path]];
            
            //两项都匹配的话  取消该请求
            if (hasMatchRequestType&&hasMatchRequestUrlString) {
                
                [operation cancel];
                
            }
        }
        
    }
}

demo地址


notice

1.以上代码仅供参考,如果有任何你觉得不对的地方,都可以联系我,我会第一时间回复,谢谢.
qq:391565521 email:zhuhaifei_ios@163.com

持续完善中,敬请期待.......

相关文章

网友评论

  • BIOTONIC:取消请求的那部分,AF3.x已经没有了常驻线程,网络请求部分都是交给NSURLSession来做的,这里的operationQueue实际上是请求完成后执行代理回调的队列,并且为了保持和AF2.x的一致性,这里也做了并发线程数为1的限制,回调的部分是异步串行,请求部分变成了多线程并发执行,并且由NSURLSession自动管理线程,和AF2.x有很大区别,之前我一直纳闷为什么请求发出去后,operationQueue.operations是空的,后来才明白这个队列里放的不是请求的task,task其实都放在session中了。
  • 4df2374eab7d:断点续传呢有吗
  • zhao1zhihui:大神。绕过我需要做后台上传以及app推出之后自动保存上传进度 下次app进入的话再次上传需要怎么做呢?afn可以实现吗?
    CharlesAir:@zhao1zhihui 你可以定义一个全局常量来记录上传的字节数 可以计算 每次程序进入前台或者后台判断这个常量 是否有值 大致的思路是这样 如果是我我会这样干
  • 最初的你我:版主知道Code4App在使用你的Demo吗??
    最初的你我:@CharlesAir http://code4app.com/thread-8424-1-1.html 没有注明转载,也没有注明作者。
    CharlesAir:不知道啊 有么 能给个链接么 麻烦你了
  • 旅行的光:你好,很感谢你的分享。我下载了你的代码,有一部分我不是太明白。在NetWorkingTools.m有一个方法是-(instancetype)initWithBaseURL:(NSURL *)url
    '''
    self.requestSerializer.timeoutInterval = 3;

    /**设置相应的缓存策略*/

    self.requestSerializer.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;


    /**分别设置请求以及相应的序列化器*/
    self.requestSerializer = [AFHTTPRequestSerializer serializer];
    '''
    这段代码通过使用self = [super initWithBaseURL:url];创建了对象。父类的initWithBaseURL: 通过调用- (instancetype)initWithBaseURL:(NSURL *)url
    sessionConfiguration:(NSURLSessionConfiguration *)configuration
    创建了对象,这个方法已经通过self.requestSerializer = [AFHTTPRequestSerializer serializer];给requestSerializer进行了赋值操作。为什么在你的方法里还要对requestSerializer进行赋值。并且[AFHTTPRequestSerializer serializer]创建的也不是一个单例。这样一来
    self.requestSerializer.timeoutInterval = 3;

    /**设置相应的缓存策略*/

    self.requestSerializer.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
    这两个赋值操作等于是没有意义了吧。
    以上是我不太明白的地方,不知道是不是我的理解有问题。谢谢
  • 190CM:取消网络请求,这个请求发出是不是需要加入队列才可以取消?
  • 庚鑫:支持断点续传下载吗 :smile:
  • 菜鸟晋升路:女程媛赞一个
  • 开创未来86:好人啊,点赞 :clap:
  • 042a0e1be73f:谢谢,学习了!
  • XDC:取消指定的url请求, requestType传什么?能详细说明下吗?
    CharlesAir:@XDC GET 或者 是 POST 这样看你要取消的请求的 请求类型
  • ef6caad47486:棒棒哒
  • da210647ad60:谢谢分享,辛苦了
    CharlesAir:@Tech_Jene 😘
  • 15afe7d7e1b2:吊吊吊 :sunglasses: :stuck_out_tongue_closed_eyes: :grin:
  • 舒耀:巾帼不让须眉,厉害
  • 小如99:正准备来集成3.0 :+1:
  • b3cb23aeeb32:mark,比我自己写的全
  • Zeke权:mark
  • 6644da2138fd:支持多个URL?
  • 唐师兄:很棒,刚好需要这个
  • i赵磊:很棒、很详细

本文标题:基于AFNetworking 3.0的集约性网络请求API再封装

本文链接:https://www.haomeiwen.com/subject/vewzrttx.html