Search code examples
iosobjective-cafnetworking

AFNetworking - reachability returning always -1


I use latest AFNetworking sources and the reachability doesn't work for me, it never fires reachability block and the [httpClient networkReachabilityStatus] always returns -1. SystemConfiguration/SystemConfiguration.h is included in .pch

startMonitoringNetworkReachability is executed (in AFHTTPClient).

iPhone 4, iOS 6.1

AFHTTPClient *httpClient = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:URL]];

[httpClient setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {

    NSLog(@"Internet status changed");
    NSLog(@"%d", status);
}];

NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:method parameters:post];

NSLog(@"Network reach %d",[httpClient networkReachabilityStatus]);

AFJSONRequestOperation *operation = [self getOperationWithMethod:method withRequest:request andCallback:callback];

[operation start];

Solution

  • Assuming you're using ARC the block is probably never being fired because your httpClient is being released as soon as this method is finished.

    To fix this you would need to create your httpClient as a strong @property such as:

    @property (nonatomic, strong) AFHTTPClient *httpClient;