Skip to content

Commit

Permalink
MOD: AFN-3.X
Browse files Browse the repository at this point in the history
Change-Id: I10e92c4b2e22f03f5436a860aca86275ebe2d25d
  • Loading branch information
liujl committed Aug 3, 2016
1 parent 73cb833 commit e3ad43b
Show file tree
Hide file tree
Showing 23 changed files with 344 additions and 200 deletions.
5 changes: 3 additions & 2 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2012-2014 YTKNetwork https://github.com/yuantiku
Copyright (c) 2012-2016 YTKNetwork https://github.com/yuantiku

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -16,4 +16,5 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
THE SOFTWARE.

49 changes: 49 additions & 0 deletions MigrationGuide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
YTKNetwork 2.0 迁移指南
======================

YTKNetwork 2.0 所依赖的 AFNetworking 版本从 2.X 变为 3.X 版本,抛弃了旧有的以 `AFHTTPRequestOperation` 为核心的 API,采用新的基于 `NSURLSession` 的 API。本指南的目的在于帮助使用 YTKNetwork 1.X 版本的应用迁移到新的 API。

## AFHTTPRequestOperation 完全被移除

在 iOS 7 上苹果引入了 `NSURLSession` 系列 API,旨在替代 `NSURLConnection` 系列 API。在 Xcode 7 中,`NSURLConnection` API 已经正式标记为废弃的(deprecated)。AFNetworking 3 当中也放弃了基于 `NSOperation` 的请求方式,转而采用基于 `NSURLSessionTask`。因此 `YTKRequest` 中的下列属性发生了变化:

#### YTKNetwork 1.X

```Objective-C
@property (nonatomic, strong) AFHTTPRequestOperation *requestOperation;
@property (nonatomic, strong, readonly, nullable) NSError *requestOperationError;
```

#### YTKNetwork 2.X

```Objective-C
@property (nonatomic, strong, readonly) NSURLSessionTask *requestTask;
@property (nonatomic, strong, readonly, nullable) NSError *error;
```

同时,原来依赖于 `AFHTTPRequestOperation` 的这些属性,需要使用新加入的替代 API 获取:

* `request.requestOperation.response` --> `request.response`
* `request.requestOperation.request` --> `request.currentRequest` & `request.originalRequest`


## 下载请求

原来基于 `AFDownloadRequestOperation` 的下载请求改为使用系统自己的 `NSURLSessionDownloadTask`。当 `YTKRequest``resumableDownloadPath` 属性不为 nil 的情况下,会调用 `NSURLSessionDownloadTask` 进行下载,下载完成后文件会自动保存到给出的路径,无需再进行存储操作。

对于下载请求来说,响应属性的获取行为如下:

* `responseData`:始终不能获取
* `responseString`:始终不能获取
* `responseObject`:为 NSURL,是下载文件在本地所存储的路径。

如果在使用 YTKNetwork 1.X 的情况下,采用存储 `responseData` 的方式进行下载,那么无需进行改动。不过对于下载完整文件的情况,建议迁移到新的采用 `NSURLSessionDownloadTask` 的 API。

同时,获取下载进度的 API 也发生了变化,旧的 block 类型被抛弃,新的 block 类型取自 AFN 3.X:

```Objective-C
typedef void (^AFURLSessionTaskProgressBlock)(NSProgress *);
```
可以通过 `NSProgress` 的 `totalUnitCount` 和 `completedUnitCount` 获取下载进度有关的信息。
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ To use YTKNetwork add the following to your Podfile

pod 'YTKNetwork'

Or add this in your Cartfile:

github "yuantiku/YTKNetwork" ~> 2.0

## Guide & Demo

* [Basic Usage Guide][BasicGuide-EN]
Expand Down Expand Up @@ -122,6 +126,12 @@ YTKNetwork 的基本的思想是把每一个网络请求封装成对象。所以

pod 'YTKNetwork'

## Carthage 支持

在 Cartfile 中加入下面的代码以使用 YTKNetwork

github "yuantiku/YTKNetwork" ~> 2.0

## 相关的使用教程和 Demo

* [基础使用教程][BasicGuide-CN]
Expand Down
7 changes: 3 additions & 4 deletions YTKNetwork.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ Pod::Spec.new do |s|
}
s.source = { :git => "https://github.com/yuantiku/YTKNetwork.git", :tag => s.version.to_s }
s.source_files = "YTKNetwork/*.{h,m}"
s.platform = :ios, '6.0'
s.private_header_files = "YTKNetwork/YTKNetworkPrivate.h"
s.platform = :ios, '7.0'
s.requires_arc = true
s.dependency "AFNetworking", "~> 2.0"
s.dependency "AFDownloadRequestOperation", "~> 2.0"

s.dependency "AFNetworking", "~> 3.0"
end
50 changes: 29 additions & 21 deletions YTKNetwork/YTKBaseRequest.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// YTKBaseRequest.h
//
// Copyright (c) 2012-2014 YTKNetwork https://github.com/yuantiku
// Copyright (c) 2012-2016 YTKNetwork https://github.com/yuantiku
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand All @@ -22,13 +22,9 @@
// THE SOFTWARE.

#import <Foundation/Foundation.h>
#import <AFNetworking/AFURLRequestSerialization.h>

NS_ASSUME_NONNULL_BEGIN

@class AFHTTPRequestOperation;
@class AFDownloadRequestOperation;

typedef NS_ENUM(NSInteger , YTKRequestMethod) {
YTKRequestMethodGet = 0,
YTKRequestMethodPost,
Expand All @@ -49,8 +45,10 @@ typedef NS_ENUM(NSInteger , YTKRequestPriority) {
YTKRequestPriorityHigh = 4,
};

@protocol AFMultipartFormData;

typedef void (^AFConstructingBlock)(id<AFMultipartFormData> formData);
typedef void (^AFDownloadProgressBlock)(AFDownloadRequestOperation *operation, NSInteger bytesRead, long long totalBytesRead, long long totalBytesExpected, long long totalBytesReadForFile, long long totalBytesExpectedToReadForFile);
typedef void (^AFURLSessionTaskProgressBlock)(NSProgress *);

@class YTKBaseRequest;

Expand Down Expand Up @@ -84,29 +82,48 @@ typedef void(^YTKRequestCompletionBlock)(__kindof YTKBaseRequest *request);
/// User info
@property (nonatomic, strong, nullable) NSDictionary *userInfo;

@property (nonatomic, strong) AFHTTPRequestOperation *requestOperation;
@property (nonatomic, strong, readonly) NSURLSessionTask *requestTask;

@property (nonatomic, strong, readonly) NSURLRequest *currentRequest;

@property (nonatomic, strong, readonly) NSURLRequest *originalRequest;

/// request delegate object
@property (nonatomic, weak, nullable) id<YTKRequestDelegate> delegate;

@property (nonatomic, strong, readonly) NSHTTPURLResponse *response;

@property (nonatomic, readonly) NSInteger responseStatusCode;

@property (nonatomic, strong, readonly, nullable) NSDictionary *responseHeaders;

@property (nonatomic, strong, readonly, nullable) NSData *responseData;

@property (nonatomic, strong, readonly, nullable) NSString *responseString;

@property (nonatomic, strong, readonly, nullable) id responseJSONObject;
@property (nonatomic, strong, readonly, nullable) id responseObject;

@property (nonatomic, readonly) NSInteger responseStatusCode;
@property (nonatomic, strong, readonly, nullable) id responseJSONObject;

@property (nonatomic, strong, readonly, nullable) NSError *requestOperationError;
@property (nonatomic, strong, readonly, nullable) NSError *error;

@property (nonatomic, copy, nullable) YTKRequestCompletionBlock successCompletionBlock;

@property (nonatomic, copy, nullable) YTKRequestCompletionBlock failureCompletionBlock;

@property (nonatomic, strong, nullable) NSMutableArray<id<YTKRequestAccessory>> *requestAccessories;

/// 当POST的内容带有文件等富文本时使用
@property (nonatomic, copy, nullable) AFConstructingBlock constructingBodyBlock;

/// 当需要断点续传时,指定续传的地址
///
/// @discussion 若设置这个属性为非 nil 路径,会使用 NSURLSessionDownloadTask 进行下载,下载完成后文件将被保存到此路径
@property (nonatomic, strong, nullable) NSString *resumableDownloadPath;

/// 当需要断点续传时,获得下载进度的回调
@property (nonatomic, copy, nullable) AFURLSessionTaskProgressBlock resumableDownloadProgressBlock;

/// 请求的优先级, 优先级高的请求会从请求队列中优先出列
@property (nonatomic) YTKRequestPriority requestPriority;

Expand Down Expand Up @@ -167,10 +184,10 @@ typedef void(^YTKRequestCompletionBlock)(__kindof YTKBaseRequest *request);
- (YTKRequestSerializerType)requestSerializerType;

/// 请求的Server用户名和密码
- (nullable NSArray *)requestAuthorizationHeaderFieldArray;
- (nullable NSArray<NSString *> *)requestAuthorizationHeaderFieldArray;

/// 在HTTP报头添加的自定义参数
- (nullable NSDictionary *)requestHeaderFieldValueDictionary;
- (nullable NSDictionary<NSString *, NSString *> *)requestHeaderFieldValueDictionary;

/// 构建自定义的UrlRequest,
/// 若这个方法返回非nil对象,会忽略requestUrl, requestArgument, requestMethod, requestSerializerType
Expand All @@ -185,15 +202,6 @@ typedef void(^YTKRequestCompletionBlock)(__kindof YTKBaseRequest *request);
/// 用于检查Status Code是否正常的方法
- (BOOL)statusCodeValidator;

/// 当POST的内容带有文件等富文本时使用
- (nullable AFConstructingBlock)constructingBodyBlock;

/// 当需要断点续传时,指定续传的地址
- (nullable NSString *)resumableDownloadPath;

/// 当需要断点续传时,获得下载进度的回调
- (nullable AFDownloadProgressBlock)resumableDownloadProgressBlock;

@end

NS_ASSUME_NONNULL_END
63 changes: 31 additions & 32 deletions YTKNetwork/YTKBaseRequest.m
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// YTKBaseRequest.m
//
// Copyright (c) 2012-2014 YTKNetwork https://github.com/yuantiku
// Copyright (c) 2012-2016 YTKNetwork https://github.com/yuantiku
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand All @@ -24,12 +24,29 @@
#import "YTKBaseRequest.h"
#import "YTKNetworkAgent.h"
#import "YTKNetworkPrivate.h"
#import "AFDownloadRequestOperation.h"
#import "AFNetworking.h"

@interface YTKBaseRequest ()

@property (nonatomic, strong, readwrite) NSURLSessionTask *requestTask;
@property (nonatomic, strong, readwrite, nullable) NSData *responseData;
@property (nonatomic, strong, readwrite, nullable) id responseJSONObject;
@property (nonatomic, strong, readwrite, nullable) id responseObject;
@property (nonatomic, strong, readwrite, nullable) NSString *responseString;
@property (nonatomic, strong, readwrite, nullable) NSError *error;

@end

@implementation YTKBaseRequest

- (NSURLRequest *)currentRequest {
return self.requestTask.currentRequest;
}

- (NSURLRequest *)originalRequest {
return self.requestTask.originalRequest;
}

/// for subclasses to overwrite
- (void)requestCompleteFilter {
}
Expand Down Expand Up @@ -98,18 +115,6 @@ - (BOOL)statusCodeValidator {
}
}

- (AFConstructingBlock)constructingBodyBlock {
return nil;
}

- (NSString *)resumableDownloadPath {
return nil;
}

- (AFDownloadProgressBlock)resumableDownloadProgressBlock {
return nil;
}

/// append self to request queue
- (void)start {
[self toggleAccessoriesWillStartCallBack];
Expand All @@ -125,11 +130,17 @@ - (void)stop {
}

- (BOOL)isCancelled {
return self.requestOperation.isCancelled;
if (!self.requestTask) {
return NO;
}
return self.requestTask.state == NSURLSessionTaskStateCanceling;
}

- (BOOL)isExecuting {
return self.requestOperation.isExecuting;
if (!self.requestTask) {
return NO;
}
return self.requestTask.state == NSURLSessionTaskStateRunning;
}

- (void)startWithCompletionBlockWithSuccess:(YTKRequestCompletionBlock)success
Expand All @@ -150,28 +161,16 @@ - (void)clearCompletionBlock {
self.failureCompletionBlock = nil;
}

- (id)responseJSONObject {
return self.requestOperation.responseObject;
}

- (NSData *)responseData {
return self.requestOperation.responseData;
}

- (NSString *)responseString {
return self.requestOperation.responseString;
- (NSHTTPURLResponse *)response {
return (NSHTTPURLResponse *)self.requestTask.response;
}

- (NSInteger)responseStatusCode {
return self.requestOperation.response.statusCode;
return self.response.statusCode;
}

- (NSDictionary *)responseHeaders {
return self.requestOperation.response.allHeaderFields;
}

- (NSError *)requestOperationError {
return self.requestOperation.error;
return self.response.allHeaderFields;
}

#pragma mark - Request Accessories
Expand Down
10 changes: 6 additions & 4 deletions YTKNetwork/YTKBatchRequest.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// YTKBatchRequest.h
//
// Copyright (c) 2012-2014 YTKNetwork https://github.com/yuantiku
// Copyright (c) 2012-2016 YTKNetwork https://github.com/yuantiku
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand All @@ -22,11 +22,13 @@
// THE SOFTWARE.

#import <Foundation/Foundation.h>
#import "YTKRequest.h"

NS_ASSUME_NONNULL_BEGIN

@class YTKRequest;
@class YTKBatchRequest;
@protocol YTKRequestAccessory;

@protocol YTKBatchRequestDelegate <NSObject>

@optional
Expand All @@ -39,9 +41,9 @@ NS_ASSUME_NONNULL_BEGIN

@interface YTKBatchRequest : NSObject

@property (strong, nonatomic, readonly) NSArray<YTKRequest *> *requestArray;
@property (nonatomic, strong, readonly) NSArray<YTKRequest *> *requestArray;

@property (weak, nonatomic, nullable) id<YTKBatchRequestDelegate> delegate;
@property (nonatomic, weak, nullable) id<YTKBatchRequestDelegate> delegate;

@property (nonatomic, copy, nullable) void (^successCompletionBlock)(YTKBatchRequest *);

Expand Down
5 changes: 3 additions & 2 deletions YTKNetwork/YTKBatchRequest.m
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// YTKBatchRequest.m
//
// Copyright (c) 2012-2014 YTKNetwork https://github.com/yuantiku
// Copyright (c) 2012-2016 YTKNetwork https://github.com/yuantiku
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand All @@ -24,6 +24,7 @@
#import "YTKBatchRequest.h"
#import "YTKNetworkPrivate.h"
#import "YTKBatchRequestAgent.h"
#import "YTKRequest.h"

@interface YTKBatchRequest() <YTKRequestDelegate>

Expand Down Expand Up @@ -137,7 +138,7 @@ - (void)requestFailed:(YTKRequest *)request {
}
// Clear
[self clearCompletionBlock];

[self toggleAccessoriesDidStopCallBack];
[[YTKBatchRequestAgent sharedInstance] removeBatchRequest:self];
}
Expand Down
Loading

0 comments on commit e3ad43b

Please sign in to comment.