Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AFNetworking 2.0 queue request when device is offline with setReachabilityStatusChangeBlock does nothing

I've been trying to come up with a solution to queue HTTP requests using AFNetworking when the device is offline, so when it goes back online the requests gets done. As far as I've been able to understand, this is possible setting the setReachabilityStatusChangeBlock: parameter.

So far this is what I have:

// ViewController.h
@interface XYZTicketViewController : UIViewController<NSURLConnectionDelegate> // This is from before I started using AFNetworking, I'm intending to change all the requests to use AFNetworking in the near future.   
@end 


// ViewController.m
(...)
#import <AFHTTPRequestOperationManager.h>
#import <AFNetworkReachabilityManager.h>
(...)
@interface XYZTicketViewController ()
- (void)viewDidLoad
(...)
{
NSURL *baseURL = [NSURL URLWithString:@"http://54.213.167.202"];
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:baseURL];

NSOperationQueue *operationQueue = manager.operationQueue;
[manager.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
    switch (status) {
        case AFNetworkReachabilityStatusReachableViaWWAN:
        case AFNetworkReachabilityStatusReachableViaWiFi:
            [operationQueue setSuspended:NO];
            NSLog(@"WIFI");
            break;
        case AFNetworkReachabilityStatusNotReachable:
        default:
            [operationQueue setSuspended:YES];
            NSLog(@"oflline, baby");
            break;
    }
}];

NSDictionary *parameters = @{@"action": @"login", @"user": @"[email protected]", @"pass": @"howdoyouturnthison"};
[manager GET:@"http://54.213.167.202/api.php"  parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];
(...)
}

I cannot find any example but I read here that it is possible, but so far anything happens when the online status changes.

Hope you can help me out

like image 844
Hito_kun Avatar asked Feb 21 '14 15:02

Hito_kun


1 Answers

You need to call startMonitoring, before you call setReachabilityStatusChangeBlock

[manager.reachabilityManager startMonitoring];

If you are using AFNetworking 2.0, I suggest following:

[[AFNetworkReachabilityManager sharedManager] startMonitoring];
[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
    DLog(@"Reachability: %@", AFStringFromNetworkReachabilityStatus(status));
    switch (status) {
        case AFNetworkReachabilityStatusReachableViaWWAN:
        case AFNetworkReachabilityStatusReachableViaWiFi:
            [operationQueue setSuspended:NO];
            NSLog(@"WIFI");
            break;
        case AFNetworkReachabilityStatusNotReachable:
        default:
            [operationQueue setSuspended:YES];
            NSLog(@"offline, baby");
            break;
    }
}];
like image 65
Kameshwar Sheoran Avatar answered Sep 20 '22 15:09

Kameshwar Sheoran