1
votes

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.

2
see this one may be helps you stackoverflow.com/questions/22980222/… - Anbu.Karthik
Use Reachability classes, they work perfectly for me - gurmandeep
now i use Reachability ,if u use AFN's isReachable right after startMonitoring,it can't get the current network status immediately. - Vergil

2 Answers

5
votes

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;
    }
}
3
votes

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.