iOS ---网络请求封装(自动缓存与手动缓存)

#import


@interface WNetworkCache : NSObject


/**

 *  手动写入/更新缓存

 *

 *  @param jsonResponse 要写入的数据

 *  @param URL    请求URL

 *

 *  @return 是否写入成功

 */

+(BOOL)saveJsonResponseToCacheFile:(id)jsonResponse andURL:(NSString *)URL;


/**

 *  获取缓存的对象

 *

 *  @param URL 请求URL

 *

 *  @return 缓存对象

 */

+(id )cacheJsonWithURL:(NSString *)URL;


@end



/**

 *  成功

 */

typedef void(^httpRequestSucess) (id responseObject);

/**

 *  失败

 */

typedef void(^httpRequestFailed) (NSError *error);


/**

 *  缓存

 */

typedef void(^httpRequestCache) (id jsonCache);



@interface XHNetworking : NSObject


/**

 *  POST请求

 *

 *  @param URL     URL String

 *  @param dic     参数

 *  @param success 成功回调

 *  @param failure 失败回调

 */

+(void)POST:(NSString *)URL parameters:(NSDictionary *)dic success:(httpRequestSucess)success failure:(httpRequestFailed)failure;



/**

 *  POST请求,自动缓存

 *

 *  @param URL       URL String

 *  @param dic       参数

 *  @param jsonCache 缓存回调

 *  @param success   成功回调

 *  @param failure   失败回调

 */

+(void)POST:(NSString *)URL parameters:(NSDictionary *)dic jsonCache:(httpRequestCache)jsonCache  success:(httpRequestSucess)success failure:(httpRequestFailed)failure;



/**

 *  GET请求

 *

 *  @param URL     URL String

 *  @param dic     参数

 *  @param success 成功回调

 *  @param failure 失败回调

 */

+(void)GET:(NSString *)URL parameters:(NSDictionary *)dic success:(httpRequestSucess)success failure:(httpRequestFailed)failure;


/**

 *  GET请求,自动缓存

 *

 *  @param URL           URL String

 *  @param dic           参数

 *  @param jsonCache 缓存回调

 *  @param success       成功回调

 *  @param failure       失败回调

 */

+(void)GET:(NSString *)URL parameters:(NSDictionary *)dic  jsonCache:(httpRequestCache)jsonCache success:(httpRequestSucess)success failure:(httpRequestFailed)failure;


@end












#import "WNetworkCache.h"

#import

#import "AFHTTPSessionManager.h"


#ifdef DEBUG

#define DebugLog(...) NSLog(__VA_ARGS__)

#else

#define DebugLog(...)

#endif



@interface WNetworkCache ()



@end



@implementation WNetworkCache


+(BOOL)saveJsonResponseToCacheFile:(id)jsonResponse andURL:(NSString *)URL

{

    NSDictionary *json = jsonResponse;

    if(json!=nil)

    {

        BOOL state =[NSKeyedArchiver archiveRootObject:jsonResponse toFile:[self cacheFilePathWithURL:URL]];

        if(state)

        {

            DebugLog(@"缓存写入/更新成功");

        }

        

        return state;

    }

    

    return NO;

}


+(id )cacheJsonWithURL:(NSString *)URL

{

    NSString *path = [self cacheFilePathWithURL:URL];

    id cacheJson;

    NSFileManager *fileManager = [NSFileManager defaultManager];

    if ([fileManager fileExistsAtPath:path isDirectory:nil] == YES) {

        cacheJson = [NSKeyedUnarchiver unarchiveObjectWithFile:path];

    }

    return cacheJson;

}


+ (NSString *)cacheFilePathWithURL:(NSString *)URL {

    

    NSString *pathOfLibrary = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];

    NSString *path = [pathOfLibrary stringByAppendingPathComponent:@"XHNetworkCache"];

    

    [self checkDirectory:path];//check路径

    DebugLog(@"path = %@",path);

    

    //文件名

    NSString *cacheFileNameString = [NSString stringWithFormat:@"URL:%@ AppVersion:%@",URL,[self appVersionString]];

    NSString *cacheFileName = [self md5StringFromString:cacheFileNameString];

    path = [path stringByAppendingPathComponent:cacheFileName];

    return path;

}


+(void)checkDirectory:(NSString *)path {

    NSFileManager *fileManager = [NSFileManager defaultManager];

    BOOL isDir;

    if (![fileManager fileExistsAtPath:path isDirectory:&isDir]) {

        [self createBaseDirectoryAtPath:path];

    } else {

        if (!isDir) {

            NSError *error = nil;

            [fileManager removeItemAtPath:path error:&error];

            [self createBaseDirectoryAtPath:path];

        }

    }

}


+ (void)createBaseDirectoryAtPath:(NSString *)path {

    __autoreleasing NSError *error = nil;

    [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES

                                               attributes:nil error:&error];

    if (error) {

        DebugLog(@"create cache directory failed, error = %@", error);

    } else {

        

        [self addDoNotBackupAttribute:path];

    }

}

+ (void)addDoNotBackupAttribute:(NSString *)path {

    NSURL *url = [NSURL fileURLWithPath:path];

    NSError *error = nil;

    [url setResourceValue:[NSNumber numberWithBool:YES] forKey:NSURLIsExcludedFromBackupKey error:&error];

    if (error) {

        DebugLog(@"error to set do not backup attribute, error = %@", error);

    }

}


+ (NSString *)md5StringFromString:(NSString *)string {

    

    if(string == nil || [string length] == 0return nil;

    

    const char *value = [string UTF8String];

    

    unsigned char outputBuffer[CC_MD5_DIGEST_LENGTH];

    CC_MD5(value, (CC_LONG)strlen(value), outputBuffer);

    

    NSMutableString *outputString = [[NSMutableString alloc] initWithCapacity:CC_MD5_DIGEST_LENGTH * 2];

    for(NSInteger count = 0; count < CC_MD5_DIGEST_LENGTH; count++){

        [outputString appendFormat:@"%02x",outputBuffer[count]];

    }

    

    return outputString;

}

+ (NSString *)appVersionString {

    

    return [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];

}

@end


#pragma mark- @interface XHNetworking

@interface XHNetworking()


@end

@implementation XHNetworking


+(AFHTTPSessionManager *)createManager

{

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

    

    //申明请求类型:HTTP

    //(AFJSONRequestSerializer,AFHTTPRequestSerializer)

    manager.requestSerializer = [AFHTTPRequestSerializer serializer];

    //申明返回的结果:JSON

    //AFJSONResponseSerializer,AFHTTPResponseSerializer

    manager.responseSerializer = [AFJSONResponseSerializer serializer];

    

    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain", nil];

    

    return manager;

}

/**

 *  POST请求

 */

+(void)POST:(NSString *)URL parameters:(NSDictionary *)dic  success:(httpRequestSucess)success failure:(httpRequestFailed)failure

{

    AFHTTPSessionManager *manager = [self createManager];

    [manager POST:URL parameters:dic progress:^(NSProgress * _Nonnull uploadProgress) {

    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

        

        success(responseObject);

        

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

        

        DebugLog(@"error=%@",error);

        

        failure(error);

    }];

}


/**

 *  POST请求,自动缓存

 */

+(void)POST:(NSString *)URL parameters:(NSDictionary *)dic jsonCache:(httpRequestCache)jsonCache  success:(httpRequestSucess)success failure:(httpRequestFailed)failure;

{

    //缓存

    jsonCache([WNetworkCache cacheJsonWithURL:URL]);

    

    AFHTTPSessionManager *manager = [self createManager];

    [manager POST:URL parameters:dic progress:^(NSProgress * _Nonnull uploadProgress) {

    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

        

        success(responseObject);

        

        [WNetworkCache saveJsonResponseToCacheFile:responseObject andURL:URL];

        

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

        

        DebugLog(@"error=%@",error);

        

        failure(error);

    }];

}


/**

 *  GET请求

 */

+(void)GET:(NSString *)URL parameters:(NSDictionary *)dic success:(httpRequestSucess)success failure:(httpRequestFailed)failure

{

    AFHTTPSessionManager *manager = [self createManager];

    [manager GET:URL parameters:dic progress:^(NSProgress * _Nonnull uploadProgress) {

    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

        

        success(responseObject);

        

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

        

        DebugLog(@"error=%@",error);

        

        failure(error);

        

    }];

}


/**

 *  GET请求,自动缓存

 */

+(void)GET:(NSString *)URL parameters:(NSDictionary *)dic  jsonCache:(httpRequestCache)jsonCache success:(httpRequestSucess)success failure:(httpRequestFailed)failure{

    

    //缓存

    jsonCache([WNetworkCache cacheJsonWithURL:URL]);

    

    AFHTTPSessionManager *manager = [self createManager];

    [manager GET:URL parameters:dic progress:^(NSProgress * _Nonnull uploadProgress) {

    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

        

        success(responseObject);

        

        [WNetworkCache saveJsonResponseToCacheFile:responseObject andURL:URL];

        

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

        

        DebugLog(@"error=%@",error);

        

        failure(error);

        

    }];

    

}



@end






你可能感兴趣的:(iOS,网络)