TWNetworkManager 1.6.0

TWNetworkManager 1.6.0

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.

Features

  • Download files with disk cache support (HTTP eTag and Last-Modified)
  • UIImage fetcher with memory and disk caching
  • Request resources without caching
  • HTTP method support: POST, GET, DELETE, PUT
  • Reachability

Why

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.

Installation

TWNetworkManager requires iOS 7 or later.

How to use it

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].

Download

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

          }];

Image download

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;
     }];

Request

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];

     }];

More method calls

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;

Example project

TWNetworkManager comes with an example project and some unit tests. Just open Example/TWNetworkManagerExample.xcworkspace

Todo

  • Test with Swift (WORKS)
  • OS X Support

Other Frameworks

Author

License

MIT