Latest 2.0.1
Homepage https://github.com/xiaozhuxiong121/PGNetworkHelper
License MIT
Platforms ios 8.0, requires ARC
Dependencies AFNetworking, PINCache
Frameworks UIKit
Authors

PGNetworkHelper

CocoaPods compatible
PGNetworkHelper PGNetworkHelper
PGNetworkHelper
PGNetworkHelper

PINCache做为AFNetworking3.x缓存层,将AFNetworking3.x请求的数据缓存起来,支持取消当前网络请求,以及取消所有的网络请求,除了常用的Get,Post方法,也将上传图片以及下载文件进行了封装,使用方法极其简单。

PGNetworkHelper屏蔽了AFNetworking自带的缓存,并将PINCache缓存的key也用MD5加密,确保数据的安全。

PGNetworkHelper也支持同步请求

AFNetworking本身就带有缓存策略,为什么要使用PINCache作为缓存呢?

第一,经过测试PINCache缓存比AFNetworking自带的缓存要快。
第二,PINCache是将缓存数据进行了加密,更加安全。

CocoaPods安装

pod 'PGNetworkHelper', '~> 2.0'

使用

#import <PGNetworkHelper/PGNetworkHelper.h>

//设置baseUrl
[PGNetAPIClient baseUrl:@"baseUrl"];
//设置SSL
[PGNetAPIClient policyWithPinningMode:AFSSLPinningModeNone];
//设置缓存路径
//多用户一般用userId来保存每个用户的缓存数据
[PGNetworkCache pathName:@"userId"];

//GET请求 只需要将cache设置为true就可以自动缓存
[PGNetworkHelper GET:@"yourUrlString" parameters:nil cache:false responseCache:nil success:^(id responseObject) {
    NSLog(@"responseObject = %@", responseObject);
} failure:^(NSError *error) {
    NSLog(@"error = %@", error);
}];

//POST请求 只需要将cache设置为true就可以自动缓存
[PGNetworkHelper POST:@"yourUrlString" parameters:@{@"username":@"test",@"password":@"test"} cache:false responseCache:nil success:^(id responseObject) {
    NSLog(@"responseObject = %@", responseObject);
} failure:^(NSError *error) {
    NSLog(@"error = %@", error);
}];

自动缓存

//只需要将cache设置为true就可以自动缓存,如果不想缓存就设置cache为false
[PGNetworkHelper GET:@"yourUrlString" parameters:nil cache:true responseCache:^(id responseCache) {
    NSLog(@"responseCache = %@", responseCache);
}  success:^(id responseObject) {
    NSLog(@"responseObject = %@", responseObject);
} failure:^(NSError *error) {
    NSLog(@"error = %@", error);
}];

使用手动缓存

如果需要将数据先进行处理,然后在缓存也是可以的。

//cache设置为true
[PGNetworkHelper GET:@"yourUrlString" parameters:nil cache:true responseCache:^(id responseCache) {
    NSLog(@"responseCache = %@", responseCache);
}  success:^(id responseObject) {
    //这里进行要缓存的数据,cacheKey就是url,如果有参数的话,就把参数拼接到cacheKey后面,下次就可以直接在responseCache block里面获取了
    [PGNetworkCache saveResponseCache:responseObject forKey:@""];
} failure:^(NSError *error) {
    NSLog(@"error = %@", error);
    }];

删除缓存

[PGNetworkCache removeResponseCacheForKey:@"cacheKey"];

删除所有的缓存

[PGNetworkCache removeAllResponseCache];

同步请求

#import <PGNetworkHelper/PGNetworkHelper+Synchronously.h>
[PGNetworkHelper synchronouslyGET:@"yourUrlString" parameters:nil cache:true responseCache:^(id responseCache) {
    NSLog(@"responseCache = %@", responseCache);
} success:^(id responseObject) {
    NSLog(@"responseObject = %@", responseObject);
} failure:^(NSError *error) {
    NSLog(@"error = %@", error);
}];

取消当前的网络请求

NSURLSessionTask *task = [PGNetworkHelper GET:@"api/user/login.json" parameters:nil cache:false responseCache:nil success:^(id responseObject) {
    NSLog(@"responseObject = %@", responseObject);
} failure:^(NSError *error) {
    NSLog(@"error = %@", error);
}];
[task cancel]; //取消当前网络请求

取消所有的网络请求

[PGNetworkHelper cancelAllOperations];

上传图片

/**
 *  上传图片文件
 *
 *  @param URL        请求地址
 *  @param parameters 请求参数
 *  @param images     图片数组
 *  @param name       文件对应服务器上的字段
 *  @param fileName   文件名
 *  @param mimeType   图片文件的类型,例:png、jpeg(默认类型)....
 *  @param progress   上传进度信息
 *  @param success    请求成功的回调
 *  @param failure    请求失败的回调
 *
 *  @return 返回的对象可取消请求,调用cancle方法
 */
+ (__kindof NSURLSessionTask *)uploadWithURL:(NSString *)URL
                                  parameters:(NSDictionary *)parameters
                                      images:(NSArray<UIImage *> *)images
                                        name:(NSString *)name
                                    fileName:(NSString *)fileName
                                    mimeType:(NSString *)mimeType
                                    progress:(HttpProgress)progress
                                     success:(HttpRequestSuccess)success
                                     failure:(HttpRequestFailed)failure;

下载文件

/**
 *  下载文件
 *
 *  @param URL      请求地址
 *  @param fileDir  文件存储目录(默认存储目录为Download)
 *  @param progress 文件下载的进度信息
 *  @param success  下载成功的回调(回调参数filePath:文件的路径)
 *  @param failure  下载失败的回调
 *
 *  @return 返回的对象可取消请求,调用cancle方法
 */
+ (__kindof NSURLSessionTask *)downloadWithURL:(NSString *)URL
                                       fileDir:(NSString *)fileDir
                                      progress:(HttpProgress)progress
                                       success:(void(^)(NSString *filePath))success
                                       failure:(HttpRequestFailed)failure;

缓存数据

[PGNetworkCache saveResponseCache:@"CacheObject" forKey:@"cacheKey"];

获取缓存数据

[PGNetworkCache getResponseCacheForKey:@"cacheKey"];

许可证

PGNetworkHelper 使用 MIT 许可证,详情见 LICENSE 文件。

Latest podspec

{
    "name": "PGNetworkHelper",
    "version": "2.0.1",
    "summary": "PINCacheu505au4e3aAFNetworkingu7f13u5b58u5c42uff0cu5c06AFNetworkingu8bf7u6c42u7684u6570u636eu7f13u5b58u8d77u6765,u652fu6301u53d6u6d88u5f53u524du7f51u7edcu8bf7u6c42uff0cu4ee5u53cau53d6u6d88u6240u6709u7684u7f51u7edcu8bf7u6c42uff0cu9664u4e86u5e38u7528u7684Getuff0cPostu65b9u6cd5uff0cu4e5fu5c06u4e0au4f20u56feu7247u4ee5u53cau4e0bu8f7du6587u4ef6u8fdbu884cu4e86u5c01u88c5uff0cu5e76u4e14u652fu6301u540cu6b65u8bf7u6c42uff0cu4f7fu7528u65b9u6cd5u53cau5176u7b80u5355u3002",
    "homepage": "https://github.com/xiaozhuxiong121/PGNetworkHelper",
    "license": "MIT",
    "authors": {
        "piggybear": "[email protected]"
    },
    "platforms": {
        "ios": "8.0"
    },
    "source": {
        "git": "https://github.com/xiaozhuxiong121/PGNetworkHelper.git",
        "tag": "2.0.1"
    },
    "source_files": [
        "PGNetworkHelper",
        "PGNetworkHelper/*.{h,m}"
    ],
    "frameworks": "UIKit",
    "requires_arc": true,
    "dependencies": {
        "AFNetworking": [],
        "PINCache": []
    }
}

Pin It on Pinterest

Share This