1

i'm using the AFN to check the network.

    __block BOOL connect;

    AFNetworkReachabilityManager*manager = [AFNetworkReachabilityManager sharedManager];
[manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
    switch (status) {
        case -1:
            connect=YES;
            break;
        case 0:
            connect=NO;
            break;
        case 1:
            connect=YES;
            break;
        case 2:
            connect=YES;
            break;
        default:
            break;
    }
}];


[manager startMonitoring];

and i want to get the Bool connect at another file

if (![ValueUtils isConnectNet])

but it didn't get the bool immediately how can i get the bool first and then do the “if else” thing?


now i use Reachability ,if u use AFN's isReachable right after startMonitoring,it can't get the current network status immediately.

Vergil
  • 13
  • 1
  • 6
  • see this one may be helps you http://stackoverflow.com/questions/22980222/how-to-check-whether-hostserver-is-available-or-not-in-ios-with-afnetworking – Anbu.Karthik Jul 15 '16 at 04:41
  • Use Reachability classes, they work perfectly for me – gurmandeep Jul 15 '16 at 05:13
  • now i use Reachability ,if u use AFN's isReachable right after startMonitoring,it can't get the current network status immediately. – Vergil Jul 15 '16 at 08:12

2 Answers2

5

You can check the reachability by this:

AFNetworkReachabilityManager *reachability = [AFNetworkReachabilityManager sharedManager];
    [reachability setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        switch (status) {
            case AFNetworkReachabilityStatusReachableViaWWAN:
                NSLog(@"WWN");
                break;
             case AFNetworkReachabilityStatusReachableViaWiFi:
                NSLog(@"WiFi");
                break;
             case AFNetworkReachabilityStatusUnknown:
                NSLog(@"Unknown");
                break;
                case AFNetworkReachabilityStatusNotReachable:
                NSLog(@"Not Reachable");
                break;
            default:
                break;
        }
    }];

or you can use:

+(BOOL)IsInternet
{
    Reachability *networkReachability = [Reachability reachabilityForInternetConnection];
    NetworkStatus networkStatus = [networkReachability currentReachabilityStatus];
    if (networkStatus == NotReachable)
    {
        return NO;
    }
    else
    {
        return YES;
    }
}
Saurabh Jain
  • 1,688
  • 14
  • 28
  • first one is AFN,and the sec one is Reachability – Vergil Jul 15 '16 at 08:16
  • Hey I've tried most of these codes but when I am connected to wifi which has no internet available, I am still getting the result as online. Is there anyway I can check if internet is available? I know reachability class is there but I am trying to explore more of AFNetworking. With out reachability is there anyway I can do ping test ? – Alex Apr 24 '18 at 06:42
3

You just need to check like this :

if ([[AFNetworkReachabilityManager sharedManager] isReachable]) 
{
    NSLog(@"IS REACHABILE");
} 
else 
{
   NSLog(@"NOT REACHABLE");
}  

No need to take bool for this. AFNetworkReachabilityManager give response that network is reachable or not.

Ekta Padaliya
  • 5,743
  • 3
  • 39
  • 51