1
AFNetworkReachabilityManager *mgr=[AFNetworkReachabilityManager sharedManager];
[mgr startMonitoring];

[mgr setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
//NSLog(@"Reachability: %@", AFStringFromNetworkReachabilityStatus(status));
    if ([AFNetworkReachabilityManager sharedManager].reachable) {
        NSLog(@" ONLINE");
    }
    else
    {
        NSLog(@"OFFLINE");
    }

}];

This is how I tested reachability through AFNetworking now! How to check reachability in specific domain? And how does AFNetworking uses to test the reachability ?

[AFNetworkReachabilityManager managerForDomain:@"www.google.com"]; didn't work

Prajeet Shrestha
  • 7,272
  • 3
  • 29
  • 51

2 Answers2

4

Couple of things.

First, you really should configure the manager before starting monitoring. In this case this means you should call setReachabilityStatusChangeBlock before calling startMonitoring.

Second, when you're creating new AFNetworkReachabilityManager by using managerForDomain:, you are responsible for managing lifetime of the object. If you use code above with ARC enabled, mgr will be deallocated as soon as it goes out of scope meaning that there will be no manager to monitor reachability. One solution is to make mgr an instance variable of a class, e.g. an application delegate.

Andrey
  • 1,541
  • 9
  • 12
  • As you suggested I created instance variable in Appdelegate like @property(strong ,nonatomic)AFNetworkReachabilityManager *mgr; and initialized mgr with mgr=[AFNetworkReachabilityManager managerForDomain:@"www.google.com"]; in appDidFinishLaunching. Now the block is running but it's showing offline. What could be the problem? – Prajeet Shrestha Aug 20 '14 at 05:14
  • Have you also updated your status change block to use `mgr` instead of `sharedManager`? If yes, then I don't know what the problem is. Try using different domain perhaps to see if it's an issue with google.com. – Andrey Aug 20 '14 at 05:22
  • Thanks a ton Andrey! I missed that. Solved the issue but if we use mgr instead of [AFNetworkReachabilityManager managerForDomain:@"www.google.com"] in if condition inside the block, it will show retain cycle warning! even if i declare mgr as @property(strong ,nonatomic)__block AFNetworkReachabilityManager *mgr; Can u suggest me about it. – Prajeet Shrestha Aug 20 '14 at 05:31
  • For breaking retain cycles please see this answer: http://stackoverflow.com/a/14556706/1524700. – Andrey Aug 20 '14 at 05:52
1

/** Creates and returns a network reachability manager for the specified domain.

@param domain The domain used to evaluate network reachability.

@return An initialized network reachability manager, actively monitoring the specified domain. */ + (instancetype)managerForDomain:(NSString *)domain;

this is from the afnetworking source code, to monitor a specified domain, just create a reachability manager for that domain using this class method. like this

AFNetworkReachabilityManager *mgr= [AFNetworkReachabilityManager managerForDomain:@"www.google.com"];
JohnHanr
  • 223
  • 2
  • 12