Skip to content

Commit dce36f2

Browse files
author
Zubin Kang
committed
Release 1.0.2
1 parent cbe8334 commit dce36f2

File tree

10 files changed

+110
-58
lines changed

10 files changed

+110
-58
lines changed

Framework/XMNetworking/XMNetworking.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ FOUNDATION_EXPORT const unsigned char XMNetworkingVersionString[];
1616

1717
// In this header, you should import all the public headers of your framework using statements like #import <XMNetworking/PublicHeader.h>
1818

19-
// Public header files for AFNetworking
19+
// Import public header files for AFNetworking
2020
#import <XMNetworking/AFURLRequestSerialization.h>
2121
#import <XMNetworking/AFURLResponseSerialization.h>
2222
#import <XMNetworking/AFSecurityPolicy.h>
@@ -25,7 +25,7 @@ FOUNDATION_EXPORT const unsigned char XMNetworkingVersionString[];
2525
#import <XMNetworking/AFURLSessionManager.h>
2626
#import <XMNetworking/AFHTTPSessionManager.h>
2727

28-
// Public header files for XMNetworking
28+
// Import public header files for XMNetworking
2929
#import <XMNetworking/XMConst.h>
3030
#import <XMNetworking/XMRequest.h>
3131
#import <XMNetworking/XMCenter.h>

README.md

Lines changed: 51 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ A lightweight but powerful network library with simplified and expressive syntax
44

55
The prefix `XM` is the abbreviation of our team [Xcode-Men](http://www.jianshu.com/users/d509cc369c78/). [中文文档](https://github.com/kangzubin/XMNetworking/blob/master/README_CN.md)
66

7-
![Platform](https://img.shields.io/badge/platform-iOS-red.svg) ![Language](https://img.shields.io/badge/language-Objective--C-orange.svg) [![CocoaPods](https://img.shields.io/badge/pod-v1.0.0-blue.svg)](http://cocoadocs.org/docsets/XMNetworking/) [![Carthage](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage) [![License](https://img.shields.io/badge/license-MIT%20License-brightgreen.svg)](https://github.com/kangzubin/XMNetworking/blob/master/LICENSE)
7+
![Platform](https://img.shields.io/badge/platform-iOS-red.svg) ![Language](https://img.shields.io/badge/language-Objective--C-orange.svg) [![CocoaPods](https://img.shields.io/badge/pod-v1.0.2-blue.svg)](http://cocoadocs.org/docsets/XMNetworking/) [![Carthage](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage) [![License](https://img.shields.io/badge/license-MIT%20License-brightgreen.svg)](https://github.com/kangzubin/XMNetworking/blob/master/LICENSE)
88

99
## Introduction
1010

11-
![](http://img.kangzubin.cn/xmnetworking/XMNetworking.png)
11+
![XMNetworking](http://img.kangzubin.cn/xmnetworking/XMNetworking.png)
1212

13-
As shown in the picture above, the XMNetworking is designed with centralization thought, all the XMRequest objects are launched and managed by XMCenter, and you could modify the callback dispatch queue and general information such as server url, header and parameter for all request through XMCenter, as well as provide a custom processing block for response data, in which you could deal with model transformation, business error code checking, network cache and so on. Futhermore, in order to switch to other network library easily or implement the underlying logic by ourself in the future, we add a XMEngine layer to insulate the dependence of third party network library like AFNetworking.
13+
As shown in the picture above, the XMNetworking is designed with centralization thought, all the XMRequest objects are launched and managed by XMCenter, and you could modify the callback dispatch queue and general information such as server url, header and parameter for all request through XMCenter, as well as provide two custom pre and post processing block, in which you could set default value for request object and deal with model transformation for response data, business error code checking, network cache and so on. Futhermore, in order to switch to other network library easily or implement the underlying logic by ourself in the future, we add a XMEngine layer to insulate the dependence of third party network library like AFNetworking.
1414

1515
## Features
1616

@@ -89,6 +89,7 @@ Download all the files in the `XMNetworking` *subdirectory*, then add the source
8989
config.generalParameters = @{@"general-parameter": @"general parameter value"};
9090
config.generalUserInfo = nil;
9191
config.callbackQueue = dispatch_get_main_queue();
92+
config.engine = [XMEngine sharedEngine];
9293
#ifdef DEBUG
9394
config.consoleLog = YES;
9495
#endif
@@ -102,6 +103,7 @@ You could configure the XMCenter througth a `XMConfig` object by invoking `+setu
102103
* **generalHeaders**: The general headers for XMCenter, if XMRequest.useGeneralHeaders is `YES` and this property is not empty, it will be appended to XMRequest.headers.
103104
* **generalUserInfo**: The general user info for XMCenter, if XMRequest.userInfo is `nil` and this property is not `nil`, it will be assigned to XMRequest.userInfo, and the `userInfo` might be used to distinguish requests with same context.
104105
* **callbackQueue**: The dispatch queue for request callback blocks. If `NULL` (default), a private concurrent queue is used.
106+
* **engine**: The global requests engine for current XMCenter object, `[XMEngine sharedEngine]` by default.
105107
* **consoleLog**: Whether to print the request and response info in console or not, `NO` by default.
106108
107109
And you could modify the general headers and parameters for XMCenter by following methods:
@@ -241,13 +243,30 @@ typedef NS_ENUM(NSInteger, XMResponseSerializerType) {
241243
242244
See also `AFURLRequestSerialization.h` and `AFURLResponseSerialization.h` .
243245
244-
### Custom Processing Logic for Response Data
246+
### Pre-process and Post-process Plugin
247+
#### Pre-process Block for Request
248+
You cloud invoke `[XMCenter setRequestProcessBlock:...]` to set pre-process plugin for XMCenter, in which you set default value for all request.
249+
250+
Note that the `requestProcessBlock` is only suitable for Normal/Upload/Download reqeusts, and it is not effective for Batch and Chain requests.
251+
252+
```objc
253+
[XMCenter setRequestProcessBlock:^(XMRequest *request) {
254+
// custom pre process logic for all requests.
255+
request.httpMethod = kXMHTTPMethodPOST;
256+
request.requestSerializerType = kXMRequestSerializerRAW;
257+
request.responseSerializerType = kXMResponseSerializerRAW;
258+
}];
259+
```
260+
261+
#### Custom Post-processing Block for Response Data
245262
Normally, the success block is called when the network reqeust finished successfully, and the failure block is called when error occurred.
246263

247264
Nonetheless, it's more likely that you might need to validate the response data or business error code agreed upon with server develorers even if the request is successfully finished.
248265

249266
Now you could invoke the `[XMCenter setResponseProcessBlock:...]` method to set a custom processing block for response data, the block is called before success block, and if the passed in `error` argument is assigned, the failure block will be called instead.
250267

268+
**In this plugin you cloud deal with model transformation for response data, business error code checking, network cache and so on.**
269+
251270
```objc
252271
[XMCenter setResponseProcessBlock:^(XMRequest *request, id responseObject, NSError *__autoreleasing *error) {
253272
// Do the custom response data processing logic by yourself.
@@ -269,16 +288,16 @@ Send batch requests concurrently, the all reqeusts are independent to each other
269288
270289
[batchRequest.requestArray addObject:request1];
271290
[batchRequest.requestArray addObject:request2];
272-
} onSuccess:^(NSArray<id> *responseObjects) {
291+
} onSuccess:^(NSArray *responseObjects) {
273292
NSLog(@"onSuccess: %@", responseObjects);
274-
} onFailure:^(NSArray<id> *errors) {
293+
} onFailure:^(NSArray *errors) {
275294
NSLog(@"onFailure: %@", errors);
276-
} onFinished:^(NSArray<id> *responseObjects, NSArray<id> *errors) {
295+
} onFinished:^(NSArray *responseObjects, NSArray *errors) {
277296
NSLog(@"onFinished");
278297
}];
279298
```
280299

281-
The `[XMCenter sendBatchRequest:...]` method return the new running `XMBatchRequest` object, and the object might be used to cancel the batch requests by invoking its `-cancelWithBlock:` method.
300+
The `[XMCenter sendBatchRequest:...]` method return a `identifier` for new running `XMBatchRequest` object, and `identifier` might be used to cancel the batch requests by invoking XMCenter's `cancelRequest:` method.
282301

283302
### Chain Requests
284303
Send chain requests one by one, the next reqeust relied on the response result of the previous reqeust, and the success block is called until all reqeusts finished, while the failure block is called once error occurred. The bool value `sendNext` is used to confirm whether to start next reqeust.
@@ -300,24 +319,24 @@ Send chain requests one by one, the next reqeust relied on the response result o
300319
request.url = @"server url 3";
301320
request.parameters = @{@"param1": @"value1", @"param2": @"value2"};
302321
}] onNext: ...];
303-
} onSuccess:^(NSArray<id> *responseObjects) {
322+
} onSuccess:^(NSArray *responseObjects) {
304323
NSLog(@"onSuccess: %@", responseObjects);
305-
} onFailure:^(NSArray<id> *errors) {
324+
} onFailure:^(NSArray *errors) {
306325
NSLog(@"onFailure: %@", errors);
307-
} onFinished:^(NSArray<id> *responseObjects, NSArray<id> *errors) {
326+
} onFinished:^(NSArray *responseObjects, NSArray *errors) {
308327
NSLog(@"onFinished");
309328
}];
310329
```
311-
The `[XMCenter sendChainRequest:...]` method return the new running `XMChainRequest` object, and the object might be used to cancel the chain requests by invoking its `-cancelWithBlock:` method.
330+
The `[XMCenter sendChainRequest:...]` method return a `identifier` for new running `XMChainRequest` object, and `identifier` might be used to cancel the chain requests by invoking XMCenter's `cancelRequest:` method.
312331
313332
### Cancel the Running Request
314333
315-
When you invoke `[XMCenter sendRequest:...]` to send a network reqeust, the method will return a unique identifier for the new running `XMRequest` object (`0` for fail), you could save the identifier value, and then cancel the running request by identifier for your business logic later if need. If a request has already finished, and your still use its identifier to cancel the request, the action will be ignored directly.
334+
When you invoke `[XMCenter sendRequest:...]` to send a network reqeust, the method will return a unique identifier for the new running `XMRequest` object (`nil` for fail), you could save the identifier value, and then cancel the running request by identifier for your business logic later if need. If a request has already finished, and your still use its identifier to cancel the request, the action will be ignored directly.
316335
317336
```objc
318337
// send a request
319-
NSUInteger identifier = [XMCenter sendRequest:^(XMRequest *request) {
320-
request.server = @"https://kangzubin.cn/";
338+
NSString identifier = [XMCenter sendRequest:^(XMRequest *request) {
339+
request.url = @"http://example.com/v1/foo/bar";
321340
request.api = @"test/index.php";
322341
request.httpMethod = kXMHTTPMethodGET;
323342
request.timeoutInterval = 10;
@@ -339,13 +358,11 @@ sleep(2);
339358
### Network Reachability
340359
There are two ways to get the network reachability:
341360
```objc
342-
[XMCenter isNetworkReachable];
361+
[[XMCenter defaultCenter] isNetworkReachable];
343362
// Return a bool value to comfirm whether network is reachable or not.
344-
```
345-
or
346-
```objc
347-
[[XMEngine sharedEngine] networkReachability];
348-
// Return the current network reachablity status, -1 to `Unknown`, 0 to `NotReachable, 1 to `WWAN` and 2 to `WiFi`
363+
364+
[[XMEngine sharedEngine] reachabilityStatus];
365+
// Return the current network reachablity status, -1 to `Unknown`, 0 to `NotReachable, 1 to `WWAN` and 2 to `WiFi`
349366
```
350367

351368
See also `AFNetworkReachabilityManager` for more details.
@@ -354,10 +371,20 @@ See also `AFNetworkReachabilityManager` for more details.
354371

355372
Adding pinned SSL certificates to your app helps prevent man-in-the-middle attacks and other vulnerabilities. Conveniently, the `AFSecurityPolicy` module could help to evaluate server trust against pinned X.509 certificates and public keys over secure connections.
356373

357-
There is a `AFHTTPSessionManager` object exposed in `XMEngine` named `sessionManager`, and you should firstly modify the `securityPolicy` mode for `sessionManager` to take SSL Pinning effective by following code, then add the `.cer` certificate file or public key to your project.
374+
In XMNetworking, we have integrated `AFSecurityPolicy` module for easy-to-use, now you could invoke the `addSSLPinningURL:` method of XMCenter to add domain name which should do SSL Pinning operation for its HTTPS requests.
375+
376+
```objc
377+
[XMCenter addSSLPinningURL:@"https://example.com/"];
378+
```
379+
380+
Then you only need to drag the .cer format certificate files to your project, it means that the bundle of .cer files should be the same with the bundle of XMNetworking's source code files. Note that if you are using XMNetworking as embedded framework (by Carthage), you need to add pinned certificates through following method:
358381
359382
```objc
360-
[XMEngine sharedEngine].sessionManager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
383+
// Add SSL Pinning Certificate
384+
NSString *certPath = [[NSBundle bundleForClass:[self class]] pathForResource:@"certificate file name" ofType:@"cer"];
385+
NSData *certData = [NSData dataWithContentsOfFile:certPath];
386+
[XMCenter addSSLPinningCert:certData];
387+
[XMCenter addSSLPinningURL:@"https://example.com/"];
361388
```
362389

363390
See also `AFSecurityPolicy` for more details.
@@ -373,7 +400,7 @@ XMNetworking includes a suite of unit tests within the `XMNetworkingDemoTests` s
373400

374401
The soure code files for XMNetworking is compact and concise, there are only four core files in the library: The `XMConst.h` defines some const enums and blocks, and `XMRequest`, `XMCenter`, `XMEngine` are the declaration and implementation for core Class, the architecture of XMNetworking is as follwing:
375402

376-
![](http://img.kangzubin.cn/xmnetworking/XMNetworking-nodes.png)
403+
![XMNetworking Structure](http://img.kangzubin.cn/xmnetworking/XMNetworking-structure.png)
377404

378405
## To Do List
379406

0 commit comments

Comments
 (0)