用RAC封装AFN

#import #import @interface MSHTTPRequest : AFHTTPSessionManager+ (instancetype _Nonnull)shareInstace; - (RACSignal * _Nonnull)POST:(NSString * _Nonnull)URLString parameters:(NSDictionary * _Nullable)parameters; @end

#import "MSHTTPRequest.h"static id _instace; @implementation MSHTTPRequest+ (instancetype)allocWithZone:(struct _NSZone *)zone { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _instace = [super allocWithZone:zone]; }); return _instace; }+ (instancetype)shareInstace { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _instace = [[self alloc] initWithBaseURL:nil]; }); return _instace; }- (id)copyWithZone:(NSZone *)zone { return _instace; }- (RACSignal * _Nonnull)POST:(NSString * _Nonnull)URLString parameters:(NSDictionary * _Nullable)parameters { return [RACSignal createSignal:^RACDisposable * _Nullable(id_Nonnull subscriber) { [self POST:URLString parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {} success:^(NSURLSessionDataTask * _Nonnull task, id_Nullable responseObject) { [subscriber sendNext:responseObject]; [subscriber sendCompleted]; } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { [subscriber sendError:error]; }]; return nil; }]; }@end

[[[MSHTTPRequest shareInstace] POST:@"url" parameters:nil] subscribeNext:^(id_Nullable x) { NSLog(@"%@", x); } error:^(NSError * _Nullable error) { NSLog(@"%@", error); }];

    推荐阅读