[AFNetworking] AFHTTPRequestOperationManagerを使うときにタイムアウト時間を設定する


## カテゴリを追加

```objectivec:AFHTTPRequestOperationManager+Timeout.m
#import <Foundation/Foundation.h>
#import "AFHTTPRequestOperationManager.h"

@interface AFHTTPRequestOperationManager (TimeoutCategory)

/**
 *  Add timeout interval parameter to AFHTTPRequestOperationManager#GET
 *
 *  @param timeoutInterval
 *  Other parameters See AFHTTPRequestOperationManager#GET
 *
 *  @return See AFHTTPRequestOperationManager#GET
 */
- (AFHTTPRequestOperation *)GET:(NSString *)URLString
                     parameters:(NSDictionary *)parameters
                timeoutInterval:(NSTimeInterval)timeoutInterval
                        success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
                        failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;

/**
 *  Add timeout interval parameter to AFHTTPRequestOperationManager#POST
 *
 *  @param timeoutInterval
 *  Other parameters See AFHTTPRequestOperationManager#POST
 *
 *  @return See AFHTTPRequestOperationManager#POST
 */
- (AFHTTPRequestOperation *)POST:(NSString *)URLString
                      parameters:(NSDictionary *)parameters
                 timeoutInterval:(NSTimeInterval)timeoutInterval
                         success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
                         failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;

@end
```

```objc:AFHTTPRequestOperationManager+Timeout.m
#import "AFHTTPRequestOperationManager+Timeout.h"

@implementation AFHTTPRequestOperationManager (TimeoutCategory)

- (AFHTTPRequestOperation *)GET:(NSString *)URLString
                     parameters:(NSDictionary *)parameters
                timeoutInterval:(NSTimeInterval)timeoutInterval
                        success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
                        failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
{
    NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:@"GET" URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters];
    [request setTimeoutInterval:timeoutInterval];
    AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
    [self.operationQueue addOperation:operation];
    
    return operation;
}

- (AFHTTPRequestOperation *)POST:(NSString *)URLString
                      parameters:(NSDictionary *)parameters
                 timeoutInterval:(NSTimeInterval)timeoutInterval
                         success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
                         failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
{
    NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:@"POST" URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters];
    [request setTimeoutInterval:timeoutInterval];
    AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
    [self.operationQueue addOperation:operation];
    
    return operation;
}

@end
```

## 使い方

```objc:sample.m
#import "AFHTTPRequestOperationManager+Timeout.h"

@implementation Sample

+ (void)fetchSamples:(NSDictionary *)params
             success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
              failed:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failed
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:@“SOME_PATH”
      parameters:@{}
 timeoutInterval:10.0f
         success:^(AFHTTPRequestOperation *operation, id responseObject) {
             success(operation, responseObject);
         } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             failed(operation, error);
         }];
}

@end
```


### [参考]
AFNetworking 2.0 のまとめ
http://qiita.com/asakahara/items/9cb68bef56ca70b505c6