如何在我的Xcode项目中添加GZIP支持

时间:2019-02-27 08:34:52

标签: objective-c gzip afnetworking

我一直在尝试如何在obj-C项目上为我的Calls添加GZIP支持,但是我似乎找不到相应的指南,

这是我的电话初始化程序

- (instancetype)initPrivate {
    self = [super init];

    if (self) {
        [[AWAFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status){
        }];
        [[AWAFNetworkReachabilityManager sharedManager] startMonitoring];

        _networkReachabilityManager = [AWAFNetworkReachabilityManager sharedManager];

        _jsonSerializer = [AWAFJSONRequestSerializer serializer];

        [_jsonSerializer setValue:[VersionUtils currentVersion] forHTTPHeaderField:@"X-Client-Version"];
        [_jsonSerializer setValue:[VersionUtils supportedOnlineCareVersions] forHTTPHeaderField:@"X-Supported-OnlineCare-Versions"];
        [_jsonSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

        _urlSerializer = [AWAFHTTPRequestSerializer serializer];

        [_urlSerializer setValue:[VersionUtils currentVersion] forHTTPHeaderField:@"X-Client-Version"];
        [_urlSerializer setValue:[VersionUtils supportedOnlineCareVersions] forHTTPHeaderField:@"X-Supported-OnlineCare-Versions"];

        if ([AWSDKService debugEnabled]) {
            [_urlSerializer setValue:@"true" forHTTPHeaderField:@"X-Internal-Build"];
            [_jsonSerializer setValue:@"true" forHTTPHeaderField:@"X-Internal-Build"];
        } else {
            [_urlSerializer setValue:@"false" forHTTPHeaderField:@"X-Internal-Build"];
            [_jsonSerializer setValue:@"false" forHTTPHeaderField:@"X-Internal-Build"];
        }
    }

    return self;
}

这是我的完整网络服务:

@interface NetworkingService ()

@property (nonatomic, readonly) AWAFNetworkReachabilityManager *networkReachabilityManager;

@property (nonatomic, readonly) AWAFJSONRequestSerializer *jsonSerializer;
@property (nonatomic, readonly) AWAFHTTPRequestSerializer *urlSerializer;
@property (nonatomic, readonly) AWJSONResponseSerializer *defaultResponseSerializer;
@property (nonatomic) NSMutableSet<NSData *> *pinnedCertificates;

@end

@implementation NetworkingService

#pragma mark - Lifecycle

+ (instancetype)sharedInstance {
    static dispatch_once_t once;
    static NetworkingService *instance;

    dispatch_once(&once, ^{
        instance = [[NetworkingService alloc] initPrivate];
        instance.pinnedCertificates = [NetworkingService defaultPinnedCertificates].mutableCopy;
    });

    return instance;
}

/**
 Searches the ENTIRE mainBundle for any cer files and returns them

 @return The found certificates. If none were found, returns empty array.
 */
+ (NSSet<NSData *> *)defaultPinnedCertificates {
    NSMutableArray<NSData *> *foundCertificates = [NSMutableArray new];
    for (NSString *path in [NSBundle.mainBundle pathsForResourcesOfType:@"cer" inDirectory:nil]) {
        [foundCertificates addObject:[NSData dataWithContentsOfFile:path]];
    }
    return [NSSet setWithArray:foundCertificates.copy];
}

+ (void)addCertificate:(NSData *)certificate {
    [NetworkingService.sharedInstance.pinnedCertificates addObject:certificate];
}

- (instancetype)initPrivate {
    self = [super init];

    if (self) {
        [[AWAFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status){
        }];
        [[AWAFNetworkReachabilityManager sharedManager] startMonitoring];

        _networkReachabilityManager = [AWAFNetworkReachabilityManager sharedManager];

        _jsonSerializer = [AWAFJSONRequestSerializer serializer];

        [_jsonSerializer setValue:[VersionUtils currentVersion] forHTTPHeaderField:@"X-Client-Version"];
        [_jsonSerializer setValue:[VersionUtils supportedOnlineCareVersions] forHTTPHeaderField:@"X-Supported-OnlineCare-Versions"];
        [_jsonSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

        _urlSerializer = [AWAFHTTPRequestSerializer serializer];

        [_urlSerializer setValue:[VersionUtils currentVersion] forHTTPHeaderField:@"X-Client-Version"];
        [_urlSerializer setValue:[VersionUtils supportedOnlineCareVersions] forHTTPHeaderField:@"X-Supported-OnlineCare-Versions"];

        if ([AWSDKService debugEnabled]) {
            [_urlSerializer setValue:@"true" forHTTPHeaderField:@"X-Internal-Build"];
            [_jsonSerializer setValue:@"true" forHTTPHeaderField:@"X-Internal-Build"];
        } else {
            [_urlSerializer setValue:@"false" forHTTPHeaderField:@"X-Internal-Build"];
            [_jsonSerializer setValue:@"false" forHTTPHeaderField:@"X-Internal-Build"];
        }
    }

    return self;
}

- (AWJSONResponseSerializer *)defaultResponseSerializer {
    // We use a custom serializer to access additional data about the response
    AWJSONResponseSerializer *responseSerializer = [AWJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
    // Redefine what constitutes "success" and "failure" - see below for details
    [responseSerializer setAcceptableStatusCodes:[self acceptableStatusCodes]];
    return responseSerializer;
}

/**
 *  Return the set of HTTP status codes that the application expects. When a network request completes, the status
 *  code of the response is used to determine whether the success or failure block is executed - anything in this
 *  set will be treated as a "success".
 *
 *  This is where we draw the line between "negative use-cases" and "network errors". If we're expecting that the
 *  server may spit back a 404/NOT FOUND, for example, and handling that case in the code, that needs to trigger
 *  the success callback so that we have the content of the response to properly proceed.
 *
 *  @return the HTTP status codes that should result in the "success" block getting executed
 */
- (NSIndexSet *)acceptableStatusCodes {
    NSMutableIndexSet *statusCodes = [NSMutableIndexSet new];

    // Treat all 200-level status codes as successful. These are HTTP spec'd to be "success" cases.
    [statusCodes addIndexesInRange:NSMakeRange(200, 99)];

    // 300-level codes are valid "redirect" cases, though we don't use these too much (if at all)
    [statusCodes addIndexesInRange:NSMakeRange(300, 99)];

    // 400 Bad Request: Validation failed - the client provided incorrect or incomplete information
    [statusCodes addIndex:400];

    // 401 Unauthorized: Not authenticated - the client wasn't properly authenticated for the request
    [statusCodes addIndex:401];

    // 403 Forbidden: Access denied - the client authenticated but isn't allowed to perform this action
    [statusCodes addIndex:403];

    // 404 Not Found: Resource missing - the client tried to access something that can't be found
    [statusCodes addIndex:404];

    // 409 Conflict: Resource in conflict - the client is attempting to act on a resource that's changed
    [statusCodes addIndex:409];

    // 410 Gone: Resource no longer exists - like 404, except the server knows for certain that it did
    // exist. This might be appropriate if the provider requests an "Appointment" that's already
    // successfully happened; the server knows that it's no longer in a "scheduled" disposition, so it
    // tells the client "Hey dummy, stop showing that thing as an appointment. Also, buy a beeper."
    [statusCodes addIndex:410];

    // 503 Service Unavailable: No access - the server cannot respond to requests right now; maybe it's
    // going down for maintenance, or the mobile app is turned off, etc.
    [statusCodes addIndex:503];

    return statusCodes;
}

- (instancetype)init {
    [self doesNotRecognizeSelector:_cmd];
    return nil;
}

#pragma mark - Public

+ (void)HEAD:(NSString *)url
     parameters:(id)parameters
        success:(NetworkSuccessBlockNoContent)success
        failure:(NetworkFailureBlock)failure
    credentials:(id<NetworkCredentials>)credentials
        options:(NSDictionary *)dict {
    [[NetworkingService sharedInstance] HEAD:url parameters:parameters success:success failure:failure credentials:credentials options:dict];
}

+ (void)GETImage:(NSString *)url success:(NetworkSuccessBlock)success failure:(NetworkFailureBlock)failure credentials:(id<NetworkCredentials>)credentials {
    [[NetworkingService sharedInstance] GETImage:url success:success failure:failure credentials:credentials];
}

+ (void)GETData:(NSString *)url success:(DataSuccessBlock)success failure:(NetworkFailureBlock)failure credentials:(id<NetworkCredentials>)credentials {
    [[NetworkingService sharedInstance] GETData:url success:success failure:failure credentials:credentials];
}

+ (void)GETData:(NSString *)url parameters:(id)parameters success:(DataSuccessBlock)success failure:(NetworkFailureBlock)failure credentials:(id<NetworkCredentials>)credentials {
    [[NetworkingService sharedInstance] GETData:url parameters:parameters success:success failure:failure credentials:credentials];
}

+ (void)GET:(NSString *)url
     parameters:(id)parameters
        success:(NetworkSuccessBlock)success
        failure:(NetworkFailureBlock)failure
    credentials:(id<NetworkCredentials>)credentials
        options:(NSDictionary *)dict {
    [[NetworkingService sharedInstance] GET:url parameters:parameters success:success failure:failure credentials:credentials options:dict];
}

+ (void)DELETE:(NSString *)url
     parameters:(id)parameters
        success:(NetworkSuccessBlock)success
        failure:(NetworkFailureBlock)failure
    credentials:(id<NetworkCredentials>)credentials
        options:(NSDictionary *)dict {
    [[NetworkingService sharedInstance] DELETE:url parameters:parameters success:success failure:failure credentials:credentials options:dict];
}

+ (void)PUT:(NSString *)url
     parameters:(id)parameters
        success:(NetworkSuccessBlock)success
        failure:(NetworkFailureBlock)failure
    credentials:(id<NetworkCredentials>)credentials
        options:(NSDictionary *)dict {
    [[NetworkingService sharedInstance] PUT:url parameters:parameters success:success failure:failure credentials:credentials options:dict];
}

+ (void)POST:(NSString *)url
     parameters:(id)parameters
        success:(NetworkSuccessBlock)success
        failure:(NetworkFailureBlock)failure
    credentials:(id<NetworkCredentials>)credentials
        options:(NSDictionary *)dict {
    [[NetworkingService sharedInstance] POST:url parameters:parameters success:success failure:failure credentials:credentials options:dict];
}

+ (void)POST:(NSString *)url
                   parameters:(id)parameters
    constructingBodyWithBlock:(void (^)(id<AFMultipartFormData> formData))block
                      success:(NetworkSuccessBlock)success
                      failure:(NetworkFailureBlock)failure
                  credentials:(id<NetworkCredentials>)credentials
                      options:(NSDictionary *)dict {
    [[NetworkingService sharedInstance] POST:url parameters:parameters constructingBodyWithBlock:block success:success failure:failure credentials:credentials options:dict];
};

+ (void)POSTMultiPart:(NSString *)url
                   parameters:(id)parameters
    constructingBodyWithBlock:(void (^)(id<AFMultipartFormData> formData))block
                     progress:(NetworkProgressBlock)progress
                      success:(NetworkSuccessBlock)success
                      failure:(NetworkFailureBlock)failure
                  credentials:(id<NetworkCredentials>)credentials
                      options:(NSDictionary *)dict {
    [[NetworkingService sharedInstance] POSTMultiPart:url parameters:parameters constructingBodyWithBlock:block progress:progress success:success failure:failure credentials:credentials options:dict];
}

+ (void)PATCH:(NSString *)url
     parameters:(id)parameters
        success:(NetworkSuccessBlock)success
        failure:(NetworkFailureBlock)failure
    credentials:(id<NetworkCredentials>)credentials
        options:(NSDictionary *)dict {
    [[NetworkingService sharedInstance] PATCH:url parameters:parameters success:success failure:failure credentials:credentials options:dict];
}

- (void)HEAD:(NSString *)url
     parameters:(id)parameters
        success:(NetworkSuccessBlockNoContent)success
        failure:(NetworkFailureBlock)failure
    credentials:(id<NetworkCredentials>)credentials
        options:(NSDictionary *)dict;
{
    AWAFHTTPRequestSerializer *requestSerializer = [dict[kUseJsonSerializer] boolValue] ? _jsonSerializer : _urlSerializer;
    AWAFHTTPResponseSerializer *responseSerializer = self.defaultResponseSerializer;

    // Overrides from options
    if ([dict[kUseAcceptHeaderVersion] intValue]) {
        NSString *acceptHeader = [NSString stringWithFormat:@"application/vnd.amwell-v%@+json", dict[kUseAcceptHeaderVersion]];
        [requestSerializer setValue:acceptHeader forHTTPHeaderField:@"Accept"];
        responseSerializer.acceptableContentTypes = [[responseSerializer acceptableContentTypes] setByAddingObject:acceptHeader];
    } else {
        [requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    }

    AWAFHTTPSessionManager *manager = [self createManagerForRequestWithCredentials:credentials requestSerialzer:requestSerializer responseSerializer:responseSerializer];
    [manager HEAD:url parameters:parameters success:success failure:[self defaultFailureBlock:failure]];
}

- (void)GETImage:(NSString *)url success:(NetworkSuccessBlock)success failure:(NetworkFailureBlock)failure credentials:(id<NetworkCredentials>)credentials {
    AWAFHTTPSessionManager *manager = [self createManagerForRequestWithCredentials:credentials requestSerialzer:_urlSerializer responseSerializer:[AWAFImageResponseSerializer serializer]];
    [manager GET:url parameters:nil success:success failure:failure];
}

- (void)GETData:(NSString *)url success:(DataSuccessBlock)success failure:(NetworkFailureBlock)failure credentials:(id<NetworkCredentials>)credentials {
    AWAFHTTPSessionManager *manager = [self createManagerForRequestWithCredentials:credentials requestSerialzer:_urlSerializer responseSerializer:[AWAFHTTPResponseSerializer serializer]];
    [manager GET:url parameters:nil success:success failure:failure];
}

- (void)GETData:(NSString *)url parameters:(id)parameters success:(DataSuccessBlock)success failure:(NetworkFailureBlock)failure credentials:(id<NetworkCredentials>)credentials {
    AWAFHTTPSessionManager *manager = [self createManagerForRequestWithCredentials:credentials requestSerialzer:_urlSerializer responseSerializer:[AWAFHTTPResponseSerializer serializer]];
    [manager GET:url parameters:parameters success:success failure:failure];
}

- (void)GET:(NSString *)url
     parameters:(id)parameters
        success:(NetworkSuccessBlock)success
        failure:(NetworkFailureBlock)failure
    credentials:(id<NetworkCredentials>)credentials
        options:(NSDictionary *)dict;
{
    AWAFHTTPRequestSerializer *requestSerializer = [dict[kUseJsonSerializer] boolValue] ? _jsonSerializer : _urlSerializer;
    AWAFHTTPResponseSerializer *responseSerializer = self.defaultResponseSerializer;

    // Overrides from options
    if ([dict[kUseAcceptHeaderVersion] intValue]) {
        NSString *acceptHeader = [NSString stringWithFormat:@"application/vnd.amwell-v%@+json", dict[kUseAcceptHeaderVersion]];
        [requestSerializer setValue:acceptHeader forHTTPHeaderField:@"Accept"];
        responseSerializer.acceptableContentTypes = [[responseSerializer acceptableContentTypes] setByAddingObject:acceptHeader];
    } else {
        [requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    }

    AWAFHTTPSessionManager *manager = [self createManagerForRequestWithCredentials:credentials requestSerialzer:requestSerializer responseSerializer:responseSerializer];
    [manager GET:url parameters:parameters success:success failure:[self defaultFailureBlock:failure]];
}

- (void)DELETE:(NSString *)url
     parameters:(id)parameters
        success:(NetworkSuccessBlock)success
        failure:(NetworkFailureBlock)failure
    credentials:(id<NetworkCredentials>)credentials
        options:(NSDictionary *)dict {
    AWAFHTTPRequestSerializer *requestSerializer = [dict[kUseJsonSerializer] boolValue] ? _jsonSerializer : _urlSerializer;
    AWAFHTTPResponseSerializer *responseSerializer = self.defaultResponseSerializer;

    // Overrides from options
    if ([dict[kUseAcceptHeaderVersion] intValue]) {
        NSString *acceptHeader = [NSString stringWithFormat:@"application/vnd.amwell-v%@+json", dict[kUseAcceptHeaderVersion]];
        [requestSerializer setValue:acceptHeader forHTTPHeaderField:@"Accept"];
        responseSerializer.acceptableContentTypes = [[responseSerializer acceptableContentTypes] setByAddingObject:acceptHeader];
    } else {
        [requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    }

    AWAFHTTPSessionManager *manager = [self createManagerForRequestWithCredentials:credentials requestSerialzer:requestSerializer responseSerializer:responseSerializer];
    [manager DELETE:url parameters:parameters success:success failure:[self defaultFailureBlock:failure]];
}

- (void)PUT:(NSString *)url
     parameters:(id)parameters
        success:(NetworkSuccessBlock)success
        failure:(NetworkFailureBlock)failure
    credentials:(id<NetworkCredentials>)credentials
        options:(NSDictionary *)dict {
    AWAFHTTPRequestSerializer *requestSerializer = [dict[kUseJsonSerializer] boolValue] ? _jsonSerializer : _urlSerializer;
    AWAFHTTPResponseSerializer *responseSerializer = self.defaultResponseSerializer;

    // Overrides from options
    if ([dict[kUseAcceptHeaderVersion] intValue]) {
        NSString *acceptHeader = [NSString stringWithFormat:@"application/vnd.amwell-v%@+json", dict[kUseAcceptHeaderVersion]];
        [requestSerializer setValue:acceptHeader forHTTPHeaderField:@"Accept"];
        responseSerializer.acceptableContentTypes = [[responseSerializer acceptableContentTypes] setByAddingObject:acceptHeader];
    } else {
        [requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    }

    AWAFHTTPSessionManager *manager = [self createManagerForRequestWithCredentials:credentials requestSerialzer:requestSerializer responseSerializer:responseSerializer];
    [manager PUT:url parameters:parameters success:success failure:[self defaultFailureBlock:failure]];
}

- (void)POST:(NSString *)url
     parameters:(id)parameters
        success:(NetworkSuccessBlock)success
        failure:(NetworkFailureBlock)failure
    credentials:(id<NetworkCredentials>)credentials
        options:(NSDictionary *)dict {
    AWAFHTTPRequestSerializer *requestSerializer = [dict[kUseJsonSerializer] boolValue] ? _jsonSerializer : _urlSerializer;
    AWAFHTTPResponseSerializer *responseSerializer = self.defaultResponseSerializer;

    // Overrides from options
    if ([dict[kUseAcceptHeaderVersion] intValue]) {
        NSString *acceptHeader = [NSString stringWithFormat:@"application/vnd.amwell-v%@+json", dict[kUseAcceptHeaderVersion]];
        [requestSerializer setValue:acceptHeader forHTTPHeaderField:@"Accept"];
        responseSerializer.acceptableContentTypes = [[responseSerializer acceptableContentTypes] setByAddingObject:acceptHeader];
    } else {
        [requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    }

    AWAFHTTPSessionManager *manager = [self createManagerForRequestWithCredentials:credentials requestSerialzer:requestSerializer responseSerializer:responseSerializer];
    [manager POST:url parameters:parameters success:success failure:[self defaultFailureBlock:failure]];
}

- (void)POSTMultiPart:(NSString *)url
                   parameters:(id)parameters
    constructingBodyWithBlock:(void (^)(id<AFMultipartFormData> formData))block
                     progress:(NetworkProgressBlock)progress
                      success:(NetworkSuccessBlock)success
                      failure:(NetworkFailureBlock)failure
                  credentials:(id<NetworkCredentials>)credentials
                      options:(NSDictionary *)dict {
    AWAFHTTPRequestSerializer *requestSerializer = [dict[kUseJsonSerializer] boolValue] ? _jsonSerializer : _urlSerializer;

    AWAFHTTPResponseSerializer *responseSerializer = self.defaultResponseSerializer;

    // Overrides from options
    if ([dict[kUseAcceptHeaderVersion] intValue]) {
        [requestSerializer setValue:[NSString stringWithFormat:@"application/vnd.amwell-v%d+json", [dict[kUseAcceptHeaderVersion] intValue]] forHTTPHeaderField:@"Accept"];
    } else {
        [requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    }

    AWAFHTTPSessionManager *manager = [self createManagerForRequestWithCredentials:credentials requestSerialzer:requestSerializer responseSerializer:responseSerializer];
    [manager POST:url parameters:parameters constructingBodyWithBlock:block success:success failure:[self defaultFailureBlock:failure]];
}

- (void)POST:(NSString *)url
                   parameters:(id)parameters
    constructingBodyWithBlock:(void (^)(id<AFMultipartFormData> formData))block
                      success:(NetworkSuccessBlock)success
                      failure:(NetworkFailureBlock)failure
                  credentials:(id<NetworkCredentials>)credentials
                      options:(NSDictionary *)dict {
    AWAFHTTPRequestSerializer *requestSerializer = [dict[kUseJsonSerializer] boolValue] ? _jsonSerializer : _urlSerializer;
    AWAFHTTPResponseSerializer *responseSerializer = self.defaultResponseSerializer;

    // Overrides from options
    if ([dict[kUseAcceptHeaderVersion] intValue]) {
        NSString *acceptHeader = [NSString stringWithFormat:@"application/vnd.amwell-v%@+json", dict[kUseAcceptHeaderVersion]];
        [requestSerializer setValue:acceptHeader forHTTPHeaderField:@"Accept"];
        responseSerializer.acceptableContentTypes = [[responseSerializer acceptableContentTypes] setByAddingObject:acceptHeader];
    } else {
        [requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    }

    AWAFHTTPSessionManager *manager = [self createManagerForRequestWithCredentials:credentials requestSerialzer:requestSerializer responseSerializer:responseSerializer];
    [manager POST:url parameters:parameters constructingBodyWithBlock:block success:success failure:[self defaultFailureBlock:failure]];
}

- (void)PATCH:(NSString *)url
     parameters:(id)parameters
        success:(NetworkSuccessBlock)success
        failure:(NetworkFailureBlock)failure
    credentials:(id<NetworkCredentials>)credentials
        options:(NSDictionary *)dict {
    AWAFHTTPRequestSerializer *requestSerializer = [dict[kUseJsonSerializer] boolValue] ? _jsonSerializer : _urlSerializer;
    AWAFHTTPResponseSerializer *responseSerializer = self.defaultResponseSerializer;

    // Overrides from options
    if ([dict[kUseAcceptHeaderVersion] intValue]) {
        NSString *acceptHeader = [NSString stringWithFormat:@"application/vnd.amwell-v%@+json", dict[kUseAcceptHeaderVersion]];
        [requestSerializer setValue:acceptHeader forHTTPHeaderField:@"Accept"];
        responseSerializer.acceptableContentTypes = [[responseSerializer acceptableContentTypes] setByAddingObject:acceptHeader];
    } else {
        [requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    }

    AWAFHTTPSessionManager *manager = [self createManagerForRequestWithCredentials:credentials requestSerialzer:requestSerializer responseSerializer:responseSerializer];

    [manager PATCH:url parameters:parameters success:success failure:[self defaultFailureBlock:failure]];
}

#pragma mark - Private

- (AWAFHTTPSessionManager *)createManagerForRequestWithCredentials:(id<NetworkCredentials>)credentials
                                                  requestSerialzer:(AWAFHTTPRequestSerializer *)requestSerializer
                                                responseSerializer:(AWAFHTTPResponseSerializer *)responseSerializer {
    AWAFHTTPSessionManager *manager = [AWAFHTTPSessionManager manager];
    __weak AWAFHTTPSessionManager *weakManager = manager;
    [manager setTaskDidCompleteBlock:^(NSURLSession *_Nonnull session, NSURLSessionTask *_Nonnull task, NSError *_Nullable error) {
        [weakManager invalidateSessionCancelingTasks:NO];
    }];

    if (AWSDKService.sharedInstance.certificatePinningEnabled) {
        manager.securityPolicy = [AWAFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
        manager.securityPolicy.pinnedCertificates = self.pinnedCertificates.allObjects;
    }

    [requestSerializer clearAuthorizationHeader];
    if (credentials) {
        [requestSerializer setValue:credentials.authorizationHeaderValue forHTTPHeaderField:@"Authorization"];
        if ([responseSerializer isKindOfClass:AWJSONResponseSerializer.class]) {
            ((AWJSONResponseSerializer *)responseSerializer).tokenDelegate = credentials;
        }
    }

    // Accept-Language HTTP Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4
    NSLocale *locale = [AWSDKLocale sharedInstance].preferredLocale;
    NSString *localeIdentifier = locale.localeIdentifier;
    NSDictionary<NSLocaleKey, NSString *> *localeComponents = [NSLocale componentsFromLocaleIdentifier:localeIdentifier];
    NSString *languageIdentifier = [NSString stringWithFormat:@"%@-%@,%@;q=1.0", localeComponents[NSLocaleLanguageCode], localeComponents[NSLocaleCountryCode], localeComponents[NSLocaleLanguageCode]];
    [requestSerializer setValue:languageIdentifier forHTTPHeaderField:@"Accept-Language"];

    [requestSerializer setValue:[[AWSDKService sharedInstance] bundleId] forHTTPHeaderField:@"X-App-Identifier"];

    [responseSerializer setAcceptableStatusCodes:[self acceptableStatusCodes]];

    [manager setRequestSerializer:requestSerializer];
    [manager setResponseSerializer:responseSerializer];

    if ([AWSDKService debugEnabled]) {
        manager.securityPolicy.allowInvalidCertificates = YES;
    }

    return manager;
}

/**
 *  Returns a default failure handler for network requests that don't have a specific one implemented.
 *  Currently, this just logs a message at WARN level and the actual error at DEBUG level; if there's
 *  additional behavior that applies across the board for unexpected request failures, here would be
 *  the place to add it.
 *
 *  @param failure the failure block that was passed into the method.
 *
 *  @return the supplied failure block, if it exists; else, a default handler
 */
- (NetworkFailureBlock)defaultFailureBlock:(NetworkFailureBlock)failure {
    if (failure) {
        return failure;
    }

    return ^(NSURLSessionDataTask *task, NSError *error) {
    };
}

@end

如何在标题中添加gzip支持?

0 个答案:

没有答案