dorin dorin - 1 year ago 153
iOS Question

iOS AFNetworking automatically retry request when the internet connection is back

Does AFNetworking for iOS offer a solution to cache failed requests (e.g due to no internet connection) and automatically retry the request when the internet connection is back.


Rob Rob
Answer Source

See the Network Reachability Manager section of the AFNetworking site. By using "Reachability", your handler will be called whenever the network availability changes.

NSURL *baseURL = [NSURL URLWithString:@""];
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:baseURL];

NSOperationQueue *operationQueue = manager.operationQueue;
[manager.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
    switch (status) {
        case AFNetworkReachabilityStatusReachableViaWWAN:
        case AFNetworkReachabilityStatusReachableViaWiFi:
            // do whatever you want when network is available
        case AFNetworkReachabilityStatusNotReachable:
            // do whatever you want when network is not available

[manager.reachabilityManager startMonitoring];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download