9

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": @"mail.address@gmail.com", @"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

Community
  • 1
  • 1
Hito_kun
  • 962
  • 3
  • 13
  • 26

2 Answers2

19

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;
    }
}];
chris
  • 16,324
  • 9
  • 37
  • 40
  • Shouldn't you logically first set the status change block and then start the monitoring? – Markus Rautopuro Jan 08 '15 at 18:31
  • I actually implemented the above code — see http://stackoverflow.com/questions/29187488/afnetworking-offline-queue but the request never executes when the device comes online again. – p0lAris Mar 21 '15 at 21:24
  • Calling before/after the status change block doesn't change anything in my case. – AnthoPak Mar 27 '18 at 13:17
0

You do not store manager. So it is like any local variable is deleted when leaving viewDidLoad. Store it to property or instance variable.

Avt
  • 16,927
  • 4
  • 52
  • 72