From 185b8e9936630cfca7ab7301cef751fba7953075 Mon Sep 17 00:00:00 2001 From: Josip Bernat Date: Mon, 28 Apr 2014 12:05:16 +0200 Subject: [PATCH] Issue #2 - Added reachability notifications. --- JBMessage/JBMessage/JBMessage.h | 35 +++++++++++++++++++++++- JBMessage/JBMessage/JBMessage.m | 47 ++++++++++++++++++++++++++++++++- 2 files changed, 80 insertions(+), 2 deletions(-) diff --git a/JBMessage/JBMessage/JBMessage.h b/JBMessage/JBMessage/JBMessage.h index 147b0b0..8b14220 100644 --- a/JBMessage/JBMessage/JBMessage.h +++ b/JBMessage/JBMessage/JBMessage.h @@ -30,6 +30,23 @@ typedef NS_ENUM(NSInteger, JBRequestSerializerType) { JBRequestSerializerTypePropertyList /// AFPropertyListRequestSerializer }; +typedef NS_ENUM(NSInteger, JBMessageReachabilityStatus) { + JBMessageReachabilityStatusUnknown = -1, /// AFNetworkReachabilityStatusUnknown + JBMessageReachabilityStatusNotReachable = 0, /// AFNetworkReachabilityStatusNotReachable + JBMessageReachabilityStatusReachableViaWiFi = 1, /// AFNetworkReachabilityStatusReachableViaWiFi + JBMessageReachabilityStatusReachableViaWWAN = 2 /// AFNetworkReachabilityStatusReachableViaWWAN +}; + +/** + * NSNotification posted when reachability status changes. UserInfo dictionary contains NSNumber with JBMessageReachabilityStatus under JBMessageReachabilityStatusKey key. + */ +extern NSString * const JBMessageReachabilityStatusChangedNotification; + +/** + * UserInfo dictionary key which contains JBMessageReachabilityStatus status value. + */ +extern NSString * const JBMessageReachabilityStatusKey; + /** * Block object containing response object and error. Used as callback when request is done with execution. * @@ -149,7 +166,23 @@ typedef void (^JBDownloadBlock)(NSUInteger bytesRead, long long totalBytesRead, */ + (void)requsterMaxNumberOfConcurrentMessages:(NSUInteger)maxConcurrentMessages; -#pragma mark - Operation Controll +#pragma mark - Reachability + +/** + * Current reachability status. + * + * @return JBMessageReachabilityStatus value holding current reachability status. + */ ++ (JBMessageReachabilityStatus)reachabilityStatus; + +/** + * Determents if internet is reachable or not using reachabilityStatus. + * + * @return Boolean value determening if internet is reachable. + */ ++ (BOOL)isInternetReachable; + +#pragma mark - Operation Control /** * Called when operation has started with the job inside NSOperationQueue. You may wish to override this method on your subclass if you need to make some aditional config before executing request. You must call super operationDidStart in order to execute request. diff --git a/JBMessage/JBMessage/JBMessage.m b/JBMessage/JBMessage/JBMessage.m index 3c2fb55..641be4e 100644 --- a/JBMessage/JBMessage/JBMessage.m +++ b/JBMessage/JBMessage/JBMessage.m @@ -14,6 +14,9 @@ JBHTTPMethod const JBHTTPMethodPUT = @"PUT"; JBHTTPMethod const JBHTTPMethodDELETE = @"DELETE"; +NSString * const JBMessageReachabilityStatusChangedNotification = @"JBMessageReachabilityStatusChangedNotification"; +NSString * const JBMessageReachabilityStatusKey = @"JBMessageReachabilityStatusKey"; + static dispatch_queue_t jb_message_completion_callback_queue() { static dispatch_queue_t completion_queue; @@ -35,6 +38,9 @@ @interface JBMessage () { #pragma mark - Shared Queue + (NSOperationQueue *)sharedQueue; +#pragma mark - Shared Instance ++ (instancetype)sharedInstance; + @property (nonatomic) UIBackgroundTaskIdentifier backgroundTaskIdentifier; @end @@ -73,6 +79,35 @@ + (NSOperationQueue *)sharedQueue { return queue; } +#pragma mark - Shared Instance + ++ (void)load { + [self sharedInstance]; +} + ++ (instancetype)sharedInstance { + + static JBMessage *instance = nil; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + instance = [[self alloc] init]; + [instance updateReachability]; + }); + return instance; +} + +- (void)updateReachability { + + [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) { + + [[NSNotificationCenter defaultCenter] postNotificationName:JBMessageReachabilityStatusChangedNotification + object:nil + userInfo:@{JBMessageReachabilityStatusKey: @(status)}]; + }]; + + [[AFNetworkReachabilityManager sharedManager] startMonitoring]; +} + #pragma mark - URL Registration static NSString *baseUrlString = nil; @@ -90,6 +125,16 @@ + (void)requsterMaxNumberOfConcurrentMessages:(NSUInteger)maxConcurrentMessages [[JBMessage sharedQueue] setMaxConcurrentOperationCount:maxConcurrentMessages]; } +#pragma mark - Reachability + ++ (JBMessageReachabilityStatus)reachabilityStatus { + return (JBMessageReachabilityStatus) [[AFNetworkReachabilityManager sharedManager] networkReachabilityStatus]; +} + ++ (BOOL)isInternetReachable { + return [[AFNetworkReachabilityManager sharedManager] isReachable]; +} + #pragma mark - Initialization + (instancetype)messageWithParameters:(NSDictionary *) parameters @@ -228,7 +273,7 @@ - (void)executeRequest { } failure:^(AFHTTPRequestOperation *operation, NSError *error) { #ifdef DEBUG NSString *response = [[NSString alloc] initWithData:operation.responseData encoding:NSUTF8StringEncoding]; - if (response) { NSLog(@"Response error: %@", response); } + if (response && response.length) { NSLog(@"Response error: %@", response); } #endif __strong JBMessage *strongThis = this; [strongThis receivedResponse:operation.responseData error:error];