Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
286 views
in Technique[技术] by (71.8m points)

ios - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...