Search code examples
iosobjective-creachability

add 3G detection to the following code


I have a method that checks for Wi-Fi that i got from an old project that i had, now i need to actually make it check for 3G or Wifi, and if none available give the error message.

Original sample working:

- (BOOL)checkForWIFIConnection {

    Reachability* wifiReach = [Reachability reachabilityForLocalWiFi];
    NetworkStatus netStatus = [wifiReach currentReachabilityStatus];
    if (netStatus!=ReachableViaWiFi)
    {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Sem conexão à internet!", @"AlertView")
                                                            message:NSLocalizedString(@"Não está conectado à internet. Tente novamente após se connectar.", @"AlertView")
                                                           delegate:self
                                                  cancelButtonTitle:NSLocalizedString(@"OK", @"AlertView")
                                                  otherButtonTitles: nil];
        [alertView show];
        return NO;
    }
    else {
        return YES;
    }
}

How do i make it check for ReachableViaWWAN ? can i just add it here (<-) ??

- (BOOL)checkForWIFIConnection {

        Reachability* wifiReach = [Reachability reachabilityForLocalWiFi];
        NetworkStatus netStatus = [wifiReach currentReachabilityStatus];
        if (netStatus!=ReachableViaWiFi && ReachableViaWWAN) <- (i get an error saying use of logical && with constant operand)
        {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Sem conexão à internet!", @"AlertView")
                                                                message:NSLocalizedString(@"Não está conectado à internet. Tente novamente após se connectar.", @"AlertView")
                                                               delegate:self
                                                      cancelButtonTitle:NSLocalizedString(@"OK", @"AlertView")
                                                      otherButtonTitles: nil];
            [alertView show];
            return NO;
        }
        else {
            return YES;
        }
    }

Thanks for your help.


Solution

  • Simply change:

    Reachability* wifiReach = [Reachability reachabilityForLocalWiFi];
    

    to:

    Reachability* wifiReach = [Reachability reachabilityForInternetConnection];
    

    and

    if (netStatus!=ReachableViaWiFi)
    

    to:

    if (netStatus == NotReachable)
    

    In other words:

    Reachability* wifiReach = [Reachability reachabilityForInternetConnection];
    NetworkStatus netStatus = [wifiReach currentReachabilityStatus];
    if (netStatus == NotReachable)
    {
    

    BTW - please consult an Objective-C tutorial to learn how to write compound expressions. Your if statement would need to be something like:

    if (netStatus!=ReachableViaWiFi && netStatus!=ReachableViaWWAN)
    

    but while this will solve the compiler issue, it won't work for your code because the WWAN value won't ever be given when using reachabilityForLocalWiFi.