Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/rockchanel/swnetwork

SWNetwork is a request util based on AFNetworking.
https://github.com/rockchanel/swnetwork

afnetworking ios network objective-c

Last synced: 5 days ago
JSON representation

SWNetwork is a request util based on AFNetworking.

Awesome Lists containing this project

README

        

# SWNetwork

[![CI Status](https://img.shields.io/travis/selwyn/SWNetwork.svg?style=flat)](https://travis-ci.org/selwyn/SWNetwork)
[![Version](https://img.shields.io/cocoapods/v/SWNetwork.svg?style=flat)](https://cocoapods.org/pods/SWNetwork)
[![License](https://img.shields.io/cocoapods/l/SWNetwork.svg?style=flat)](https://cocoapods.org/pods/SWNetwork)
[![Platform](https://img.shields.io/cocoapods/p/SWNetwork.svg?style=flat)](https://cocoapods.org/pods/SWNetwork)

## Example

To run the example project, clone the repo, and run `pod install` from the Example directory first.

## Installation

SWNetwork is available through [CocoaPods](https://cocoapods.org). To install
it, simply add the following line to your Podfile:

```ruby
pod 'SWNetwork'
```

## Usage

### SWRequest

#### Creating a Data Task

```objective-c
[[SWNetworkAgent request:^(SWRequest * _Nonnull request) {
request.httpMethod = SWHTTPMethodGET;
request.path = @"service/regeo";
request.parameters = @{
@"longitude": @"119.04925573429551",
@"latitude": @"31.315590522490712"
};
}] startWithSuccess:^(SWRequest * _Nonnull request) {
NSLog(@"request === %@", request.responseObject);
} failure:^(SWRequest * _Nonnull request) {
NSLog(@"request === %@", request.error);
} completed:^(SWRequest * _Nonnull request) {

}];
```

#### Creating a Download Task

```objective-c
NSString *downloadDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
SWRequest *request = [SWRequest request];
request.downloadPath = downloadDir;
request.path = @"http://dldir1.qq.com/qqfile/QQforMac/QQ_V5.4.0.dmg";
request.progressBlock = ^(NSProgress * _Nonnull progress) {
CGFloat stauts = 100.f * progress.completedUnitCount/progress.totalUnitCount;
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"download test progress ==== %@", [NSString stringWithFormat:@"%.2f", stauts/100.f]);
});
};
[request startWithSuccess:^(SWRequest * _Nonnull request) {
NSLog(@"download test path === %@", request.responseObject);
} failure:^(SWRequest * _Nonnull request) {

} completed:^(SWRequest * _Nonnull request) {

}];
```

------

### SWBatchRequest

```objective-c
[[SWNetworkAgent batchRequest:^(SWBatchRequest * _Nonnull request) {
NSMutableArray *requests = [NSMutableArray array];

SWRequest *tmpReq1 = [SWRequest request];
tmpReq1.httpMethod = SWHTTPMethodGET;
tmpReq1.path = @"service/regeo";
tmpReq1.parameters = @{
@"longitude": @"119.04925573429551",
@"latitude": @"31.315590522490712"
};
tmpReq1.tag = 100;
[requests addObject:tmpReq1];

SWCustomRequest *tmpReq2 = [SWCustomRequest request];
tmpReq2.tag = 200;
[requests addObject:tmpReq2];

request.requests = requests;
}] startWithSuccess:^(SWBatchRequest * _Nonnull request) {
// for (SWRequest *req in request.requests) {
// if (req.tag == 100) {
// NSLog(@"tmpReq1 == %@", req.responseObject);
// }
// else {
// NSLog(@"tmpReq2 == %@", req.responseObject);
// }
// }

// Get request by index
NSLog(@"tmpReq1 == %@", request.requests[0].responseObject);
NSLog(@"tmpReq2 == %@", request.requests[1].responseObject);
} failure:^(SWBatchRequest * _Nonnull request) {
NSLog(@"tmpReq1 == %@", request.requests[0].error);
NSLog(@"tmpReq2 == %@", request.requests[1].error);
} completed:^(SWBatchRequest * _Nonnull request) {

}];
```

------

### SWChainRequest

```objective-c
[[SWNetworkAgent chainRequest:^(SWChainRequest * _Nonnull request) {
SWRequest *tmpReq1 = [SWRequest request];
tmpReq1.httpMethod = SWHTTPMethodGET;
tmpReq1.path = @"service/regeo";
tmpReq1.parameters = @{
@"longitude": @"119.04925573429551",
@"latitude": @"31.315590522490712"
};
tmpReq1.tag = 100;

[request nextRequest:tmpReq1 block:^(SWRequest * _Nonnull currentRequest) {
NSLog(@"tmpReq1 == %@", currentRequest.responseObject);
SWCustomRequest *tmpReq2 = [SWCustomRequest request];
tmpReq2.tag = 200;

[request nextRequest:tmpReq2 block:^(SWRequest * _Nonnull currentRequest) {
NSLog(@"tmpReq2 == %@", currentRequest.responseObject);
}];
}];
}] startWithSuccess:^(SWChainRequest * _Nonnull request) {

} failure:^(SWChainRequest * _Nonnull request) {

} completed:^(SWChainRequest * _Nonnull request) {

}];
```

------

### SWBatchChainRequest

```objective-c
[[SWNetworkAgent batchChainRequest:^(SWBatchChainRequest * _Nonnull request) {
SWBatchRequest *batchReq1 = [SWBatchRequest request];
NSMutableArray *requests = [NSMutableArray array];

SWRequest *tmpReq1 = [SWRequest request];
tmpReq1.httpMethod = SWHTTPMethodGET;
tmpReq1.path = @"service/regeo";
tmpReq1.parameters = @{
@"longitude": @"119.04925573429551",
@"latitude": @"31.315590522490712"
};
tmpReq1.tag = 100;
[requests addObject:tmpReq1];

SWCustomRequest *tmpReq2 = [SWCustomRequest request];
tmpReq2.tag = 200;
[requests addObject:tmpReq2];
batchReq1.requests = requests;

[request nextRequest:batchReq1 block:^(SWBatchRequest * _Nonnull currentRequest) {
SWBatchRequest *batchReq2 = [SWBatchRequest request];
NSMutableArray *requests = [NSMutableArray array];

SWRequest *tmpReq1 = [SWRequest request];
tmpReq1.httpMethod = SWHTTPMethodGET;
tmpReq1.path = @"service/regeo";
tmpReq1.parameters = @{
@"longitude": @"119.04925573429551",
@"latitude": @"31.315590522490712"
};
tmpReq1.tag = 100;
[requests addObject:tmpReq1];

SWCustomRequest *tmpReq2 = [SWCustomRequest request];
tmpReq2.tag = 200;
[requests addObject:tmpReq2];
batchReq2.requests = requests;

[request nextRequest:batchReq2 block:^(SWBatchRequest * _Nonnull currentRequest) {

}];
}];

}] startWithSuccess:^(SWBatchChainRequest * _Nonnull request) {

} failure:^(SWBatchChainRequest * _Nonnull request) {

} completed:^(SWBatchChainRequest * _Nonnull request) {

}];
```

------

### Security Policy

Adding pinned SSL certificates to your app helps prevent man-in-the-middle attacks and other vulnerabilities.

#### Allowing Invalid SSL Certificates

```objective-c
SWNetworkConfiguration *config = [SWNetworkConfiguration sharedConfiguration];

AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
securityPolicy.allowInvalidCertificates = YES;
securityPolicy.validatesDomainName = NO;
config.securityPolicy = securityPolicy;
```

------

### SWNetworkManager

Monitors the reachability of domains, and addresses for both WWAN and WiFi network interfaces.

#### Shared Network Reachability

```objective-c
[SWNetworkManager networkStatusWithBlock:^(SWNetworkReachabilityStatus status) {
NSLog(@"Network Status Changed");
}];
```

------

## Author

selwyn, [email protected]

## License

SWNetwork is available under the MIT license. See the LICENSE file for more info.