You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+51-24Lines changed: 51 additions & 24 deletions
Original file line number
Diff line number
Diff line change
@@ -4,13 +4,13 @@ A lightweight but powerful network library with simplified and expressive syntax
4
4
5
5
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)
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.
14
14
15
15
## Features
16
16
@@ -89,6 +89,7 @@ Download all the files in the `XMNetworking` *subdirectory*, then add the source
@@ -102,6 +103,7 @@ You could configure the XMCenter througth a `XMConfig` object by invoking `+setu
102
103
* **generalHeaders**: The general headers for XMCenter, if XMRequest.useGeneralHeaders is `YES` and this property is not empty, it will be appended to XMRequest.headers.
103
104
* **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.
104
105
* **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.
105
107
* **consoleLog**: Whether to print the request and response info in console or not, `NO` by default.
106
108
107
109
And you could modify the general headers and parameters for XMCenter by following methods:
#### Custom Post-processing Block for Response Data
245
262
Normally, the success block is called when the network reqeust finished successfully, and the failure block is called when error occurred.
246
263
247
264
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.
248
265
249
266
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.
250
267
268
+
**In this plugin you cloud deal with model transformation for response data, business error code checking, network cache and so on.**
269
+
251
270
```objc
252
271
[XMCenter setResponseProcessBlock:^(XMRequest *request, id responseObject, NSError *__autoreleasing *error) {
253
272
// 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
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.
282
301
283
302
### Chain Requests
284
303
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
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.
312
331
313
332
### Cancel the Running Request
314
333
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.
There are two ways to get the network reachability:
341
360
```objc
342
-
[XMCenter isNetworkReachable];
361
+
[[XMCenterdefaultCenter]isNetworkReachable];
343
362
// 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`
349
366
```
350
367
351
368
See also `AFNetworkReachabilityManager` for more details.
@@ -354,10 +371,20 @@ See also `AFNetworkReachabilityManager` for more details.
354
371
355
372
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.
356
373
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.
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:
@@ -373,7 +400,7 @@ XMNetworking includes a suite of unit tests within the `XMNetworkingDemoTests` s
373
400
374
401
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:
0 commit comments