TestsTested | ✗ |
LangLanguage | Obj-CObjective C |
License | MIT |
ReleasedLast Release | Feb 2018 |
Maintained by Christian Menschel.
#####TWNetworkManager is a lightweight Objective-C network resource download library with caching support based on NSURLSession.
TWNetworkManager is a wrapper for NSURLSession with some extras and convenience methods. The purpose is NOT to replace AFNetworking. I just wanted to have a simple NSURLSession wrapper with caching support that everyone else can adapt easily.
TWNetworkManager requires iOS 7 or later.
Example are still in Objective C. Sure, Swift works as well. Make sure to import the header file
#import <TWNetworkManager/TWNetworkManager.h>
The defaultManager
is the standard singleton instance. But TWNetworkManager can be used also as non singleton with [[TWNetworkManager alloc] init]
.
This method uses disk caching with HTTP eTag
and Last-Modified
.
NSURL *url = [NSURL URLWithString:@"http://lorempixel.com/700/300/"];
[[TWNetworkManager defaultManager]
downloadURL:url
completion:^(NSData *data,
NSString *localFilepath,
BOOL isFromCache,
NSError *error) {
// Do something with the data
}];
It's a more convient method to get an UIImage.
It uses memory and also disk caching with HTTP eTag
and Last-Modified
.
NSURL *url = [NSURL URLWithString:@"http://lorempixel.com/700/300/"];
[[TWNetworkManager defaultManager]
imageAtURL:url
completion:^(UIImage *image,
NSString *localFilepath,
BOOL isFromCache,
NSError *error) {
self.imageView.image = image;
}];
This starts the download without any disk caching.
As parameter you can pass the HTTP methods:
GET : TWNetworkHTTPMethodGET
POST : TWNetworkHTTPMethodPOST
PUT : TWNetworkHTTPMethodPUT
DELETE : TWNetworkHTTPMethodDELETE
NSURL *url = [NSURL URLWithString:@"http://whatthecommit.com"];
[[TWNetworkManager defaultManager]
requestURL:url
type:TWNetworkHTTPMethodGET
completion:^(NSData *data,
NSString *localFilepath,
BOOL isFromCache,
NSError *error) {
NSString *html = [[NSString alloc]
initWithData:data
encoding:NSASCIIStringEncoding];
}];
This resets the memory cache and deletes all cached data on disk
- (BOOL)reset;
This cancels all running requests
- (BOOL)cancelAllRequests;
Returns a path of a cached file for a given NSURL
- (NSString *)cachedFilePathForURL:(NSURL *)url;
Returns YES if there is a cached file on disk for the given NSURL
- (BOOL)hasCachedFileForURL:(NSURL *)url;
Returns YES if the given NSURL is currently being progressed
- (BOOL)isProcessingURL:(NSURL *)url;
Good old Reachability
@property (nonatomic, readonly) BOOL isNetworkReachable;
@property (nonatomic, readonly) BOOL isReachableViaWiFi;
TWNetworkManager comes with an example project and some unit tests. Just open Example/TWNetworkManagerExample.xcworkspace