Latest 1.2.1
Homepage https://github.com/spirit-jsb/JSNetworking
License MIT
Platforms ios 8.0
Dependencies AFNetworking
Authors

Version
License
Platform

JSNetworking: 轻量级网络层封装, 基于 AFNetworking 3.1.0

导入方法

pod 'JSNetworking', '~> 1.2.1'

使用方法

头文件的导入

#import "JSNetworking.h"

全局网络配置

[JSCenterManager setupConfig:^(JSConfigManager *config) {
    config.commenServer = @"commen server address";
    config.commenHeaders = @{@"commen header": @"commen header value"};
    config.commenParameters = @{@"commen parameters": @"commen parameters value"};
    config.commenUserInfo = nil;
    config.callbackQueue = dispatch_get_main_queue();
#ifdef DEBUG
    config.consoleLog = YES;
#endif
}];

你可以通过调用 JSCenterManager 的以下两个类方法来随时修改全局公共的 headerparameter:

+ (void)setCommenHeaderValue:(NSString *)value
                      forKey:(NSString *)key;

+ (void)setCommenParameterValue:(NSString *)value
                         forKey:(NSString *)key;

普通请求

GET

[JSCenterManager sendRequest:^(JSRequestManager *request) {
    request.httpMethod = kJSHTTPMethodGET;

    request.url = @"url";
    // request.server = @"server";
    // request.api = @"api";

    request.parameters = @{@"param1": @"value1", @"param2": @"value2"};
    request.headers = @{@"header1": @"value1"};
} onSuccess:^(id responseObject) {
    NSLog(@"onSuccess: %@", responseObject);
} onFailure:^(NSError *error) {
    NSLog(@"onFailure: %@", error);
} onFinished:^(id responseObject, NSError *error) {
    NSLog(@"onFinished");
}];

POST

[JSCenterManager sendRequest:^(JSRequestManager *request) {        
    request.url = @"url";
    // request.server = @"server";
    // request.api = @"api";

    request.parameters = @{@"param1": @"value1", @"param2": @"value2"};
    request.headers = @{@"header1": @"value1"};
} onSuccess:^(id responseObject) {
    NSLog(@"onSuccess: %@", responseObject);
} onFailure:^(NSError *error) {
    NSLog(@"onFailure: %@", error);
} onFinished:^(id responseObject, NSError *error) {
    NSLog(@"onFinished");
}];

其他 HTTP 方法

使用方法与上述类似, 不在赘述

批量请求

[JSCenterManager sendBatchRequest:^(JSBatchRequestManager *batchRequest) {
    JSRequestManager *request1 = [JSRequestManager request];
    request1.url = @"url1";
    ...

    JSRequestManager *request2 = [JSRequestManager request];
    request2.url = @"url2";
    ...

    [batchRequest.requestArray addObject:request1];
    [batchRequest.requestArray addObject:request2];
} onSuccess:^(NSArray<id> *responseObjects) {
    NSLog(@"onSuccess: %@", responseObjects);
} onFailure:^(NSArray<id> *errors) {
    NSLog(@"onFaulure: %@", errors);
} onFinished:^(NSArray<id> *responseObjects, NSArray<id> *errors) {
    NSLog(@"onFinished");
}];

链式请求

[JSCenterManager sendChainRequest:^(JSChainRequestManager *chainRequest) {
    [[[chainRequest onFirst:^(JSRequestManager *request) {
        request.url = @"url1";
        ...
    }] onNext:^(JSRequestManager *request, id responseObject, BOOL *sendNext) {
        request.url = @"url2";
        ...
    }] onNext:...];
} onSuccess:^(NSArray<id> *responseObjects) {
    NSLog(@"onSuccess: %@", responseObjects);
} onFailure:^(NSArray<id> *errors) {
    NSLog(@"onFailure: %@", errors);
} onFinished:^(NSArray<id> *responseObjects, NSArray<id> *errors) {
    NSLog(@"onFinished");
}];

上传请求

// `NSData` form data.
UIImage *image = [UIImage imageNamed:@"testImage"];
NSData *fileData1 = UIImageJPEGRepresentation(image, 1.0);
// `NSURL`  form data.
NSString *path = [NSHomeDirectory() stringByAppendingString:@"/Documents/testImage.png"];
NSURL *fileURL2 = [NSURL fileURLWithPath:path isDirectory:NO];

[JSCenterManager sendRequest:^(JSRequestManager *request) {
    request.requestType = kJSRequestUpload;

    request.server = @"server";
    request.api = @"api";

    [request addFormDataWithName:@"image"
                        fileName:@"temp.jpg"
                        mimeType:@"image/jpeg"
                        fileData:fileData1];

    [request addFormDataWithName:@"image"
                         fileURL:fileURL2];
} onProgress:^(NSProgress *progress) {
    if (progress) {
        NSLog(@"onProgress: %f", progress.fractionCompleted);
    }
} onSuccess:^(id responseObject) {
    NSLog(@"onSuccess: %@", responseObject);
} onFailure:^(NSError *error) {
    NSLog(@"onFailure: %@", error);
} onFinished:^(id responseObject, NSError *error) {
    NSLog(@"onFinished");
}];

下载请求

[JSCenterManager sendRequest:^(JSRequestManager *request) {
    request.requestType = kJSRequestDownload;

    request.url = @"http://example.com/v1/testDownFile.zip";
    request.downloadSavePath = [NSHomeDirectory() stringByAppendingString:@"/Documents/"];
} onProgress:^(NSProgress *progress) {
    if (progress) {
        NSLog(@"onProgress: %f", progress.fractionCompleted);
    }
} onSuccess:^(id responseObject) {
    NSLog(@"onSuccess: %@", responseObject);
} onFailure:^(NSError *error) {
    NSLog(@"onFailure: %@", error);
}];

取消请求

当调用 [JSCenterManager sendRequest:...] 方法发送一个网络请求时, 该方法会返回一个用于唯一标识该请求对象的 identifier (如果请求发送失败, 该值为 0) , 在必要的时候, 你可以通过这个 identifier 来取消当前网络请求 (如果一个请求已经结束, 这时再用 identifier 来取消该请求时, 会直接忽略)

[JSCenterManager cancelRequest:identifier
                      onCancel:^(JSCenterManager *request) {
                        NSLog(@"onCancel");
                      }];

网络可连接性检查

// 该方法会返回一个 BOOL 值用于表示当前网络是否可连接

[JSCenterManager isNetworkReachable];
// 该方法会返回一个当前网络的状态值, -1 表示 `Unknown`, 0 表示 `NotReachable, 1 表示 `WWAN`, 2 表示 `WiFi`

[[JSCoreManager sharedManager] networkReachabilityStatus];

详见 AFNetworkReachabilityManager.h 获取更多细节

HTTPS 请求的本地证书校验 (SSL Pinning)

在你的应用程序包里添加 (pinned) 相应的 SSL 证书做校验有助于防止中间人攻击和其他安全漏洞
非常方便的是, AFNetworking 的 AFSecurityPolicy 安全模块可以通过校验本地保存的证书或公钥帮助我们评估服务器是否可信任以及建立安全连接

我们在 JSCoreManager 中暴露了一个 AFHTTPSessionManager 对象叫 sessionManager,
你可以通过修改该对象的 securityPolicy 类型,以开启 SSL Pinning 功能, 并把你们服务器对应的 .cer 证书或者公钥放到你的工程中

[JSCoreManager sharedManager].sessionManager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];

详见 AFSecurityPolicy.h 获取更多细节

作者

spirit-jsb, [email protected]

许可协议

JSNetworking 遵循 MIT 开源许可证, 详情请查看 LICENSE 文件

Latest podspec

{
    "name": "JSNetworking",
    "version": "1.2.1",
    "summary": "JSNetworking: u8f7bu91cfu7ea7u7f51u7edcu5c42u5c01u88c5, u57fau4e8e AFNetworking 3.1.0",
    "homepage": "https://github.com/spirit-jsb/JSNetworking",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "spirit-jsb": "[email protected]"
    },
    "source": {
        "git": "https://github.com/spirit-jsb/JSNetworking.git",
        "tag": "1.2.1"
    },
    "platforms": {
        "ios": "8.0"
    },
    "source_files": "JSNetworking/Classes/**/*.{h,m}",
    "dependencies": {
        "AFNetworking": [
            "~> 3.1.0"
        ]
    }
}

Pin It on Pinterest

Share This