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
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…