美文网首页
AFNetworkManager

AFNetworkManager

作者: 863cda997e42 | 来源:发表于2017-12-25 10:54 被阅读10次
    @interface AFNetworkManager : NSObject
    
    @property (nonatomic, strong) AFHTTPSessionManager *engine;
    
    /**
     *  Description
     *
     *  @return return value description
     */
    + (AFNetworkManager *)sharedManager;
    + (AFNetworkManager *)sharedManagerWithGBK;
    + (AFNetworkManager *)sharedManagerWithUTF8;
    + (AFNetworkManager *)sharedManagerWithEncoding:(NSStringEncoding) encoding;
    
    /**
     *  Description
     *
     *  @param resource resource description
     *  @param params   params description
     *  @param event    event description
     */
    
    - (void)requestServiceWithResource:(NSString *) resource
                                params:(NSDictionary *) params
                                 event:(void (^)(NSURLSessionTask *task, id responseJSON)) event
                          errorHandler:(void (^)(NSURLSessionTask *task, NSError *error)) errorHandler;
    
    -(void) sendAsyncRequest:(NSURLRequest *)request
              completionHandler:(nullable void (^)(NSURLResponse *response, id _Nullable responseObject,  NSError * _Nullable error))completionHandler;
    
    @end
    
    
    #import "AFNetworkManager.h"
    
    @implementation AFNetworkManager
    
    @synthesize engine;
    
    #pragma mark - Singleton Methods
    
    + (AFNetworkManager *)sharedManager {
    //    static AFNetworkManager *_sharedManager = nil;
    //    static dispatch_once_t onceToken;
    //    dispatch_once(&onceToken, ^{
    //        _sharedManager = [[self alloc] init];
    //    });
    //    return _sharedManager;
        return [AFNetworkManager sharedManagerWithGBK];
    }
    
    + (AFNetworkManager *)sharedManagerWithGBK {
        static AFNetworkManager *_sharedManagerWithGBK = nil;
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            _sharedManagerWithGBK = [[self alloc] initWithEncoding:GBK_ENCODING];
        });
        return _sharedManagerWithGBK;
    }
    
    + (AFNetworkManager *)sharedManagerWithUTF8 {
        static AFNetworkManager *_sharedManagerWithUTF8 = nil;
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            _sharedManagerWithUTF8 = [[self alloc] initWithEncoding:NSUTF8StringEncoding];
        });
        return _sharedManagerWithUTF8;
    }
    
    + (AFNetworkManager *)sharedManagerWithEncoding:(NSStringEncoding) encoding {
        static AFNetworkManager *_sharedManagerWithEncoding = nil;
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            _sharedManagerWithEncoding = [[self alloc] initWithEncoding:encoding];
        });
        _sharedManagerWithEncoding.engine.responseSerializer.stringEncoding = encoding;
        return _sharedManagerWithEncoding;
    }
    
    //- (id)init {
    //    if (self = [super init]) {
    //        NSURLSessionConfiguration *defaultConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
    //        
    //        self.engine = [[AFHTTPSessionManager alloc]
    //                            initWithBaseURL:[NSURL URLWithString:g_WebServiceHost]
    //                       sessionConfiguration:defaultConfig];
    //        self.engine.responseSerializer.stringEncoding = GBK_ENCODING;
    //        self.engine.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/plain", nil];
    //    }
    //    return self;
    //}
    
    - (id)initWithEncoding:(NSStringEncoding) encoding {
        if (self = [super init]) {
            NSURLSessionConfiguration *defaultConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
            
            self.engine = [[AFHTTPSessionManager alloc]
                           initWithBaseURL:[NSURL URLWithString:g_WebServiceHost]
                           sessionConfiguration:defaultConfig];
            self.engine.responseSerializer.stringEncoding = encoding;
            self.engine.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/plain", nil];
        }
        return self;
    }
    
    - (void)dealloc {
        // Should never be called, but just here for clarity really.
    }
    
    - (void) requestServiceWithResource:(NSString *) resource
                                params:(NSDictionary *) params
                                 event:(void (^)(NSURLSessionTask *task, id responseJSON)) event
                          errorHandler:(void (^)(NSURLSessionTask *task, NSError *error)) errorHandler{
        
        NSString *requestUrl = [UrlPath stringByAppendingString:resource];
        
        NSString * url = [NSString stringWithFormat:@"http://%@/%@", g_WebServiceHost, requestUrl];
        [self.engine GET:url parameters:params progress:nil
                 success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                     event(task, responseObject);
        }
                 failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                     errorHandler(task, error);
        }];
    }
    
    //-(void) sendAsyncRequest:(NSURLRequest *)request completionHandler:(nullable void (^)(NSURLResponse *response, id _Nullable responseObject,  NSError * _Nullable error))completionHandler{
    //    [self.engine dataTaskWithRequest:request uploadProgress:nil downloadProgress:nil completionHandler:completionHandler];
    //}
    
    -(void) sendAsyncRequest:(NSURLRequest *)request completionHandler:(nullable void (^)(NSURLResponse *response, id _Nullable responseObject,  NSError * _Nullable error))completionHandler{
        NSURLSessionDataTask *task = [self.engine dataTaskWithRequest:request uploadProgress:nil downloadProgress:nil completionHandler:completionHandler];
        [task resume];
    }
    
    @end
    
    

    相关文章

      网友评论

          本文标题:AFNetworkManager

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