Question

.h

- (void)checkForWIFIConnection;

.m

- (void)checkForWIFIConnection {
    Reachability* wifiReach = [Reachability reachabilityForLocalWiFi];
    NetworkStatus netStatus = [wifiReach currentReachabilityStatus];
    if (netStatus!=ReachableViaWiFi)
    {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"No WIFI available!", @"AlertView")
                                                            message:NSLocalizedString(@"You have no wifi connection available. Please connect to a WIFI network.", @"AlertView")
                                                           delegate:self
                                                  cancelButtonTitle:NSLocalizedString(@"Cancel", @"AlertView")
                                                  otherButtonTitles:NSLocalizedString(@"Open settings", @"AlertView"), nil];
        [alertView show];
    }
}



- (void)viewDidLoad {
    [super viewDidLoad];

    [self checkForWIFIConnection]; // this does not show an alert...
    [self parseXML]; // ...if this is here, but if i remove this line i get the UIALert saying no INTERNET

...
}

How do i make him only parse if the checkForInternetConnection does not fail?

I assume it's like if self Checkforwifi... { do this } else { do that}

But my checkforwifi is void, and does not return BOOL, i tried to change the method but since i'm kinda fairly new, i failed miserably.

Any help?

Cheers

Was it helpful?

Solution

- (BOOL)checkForWIFIConnection {
    Reachability* wifiReach = [Reachability reachabilityForLocalWiFi];
    NetworkStatus netStatus = [wifiReach currentReachabilityStatus];
    if (netStatus!=ReachableViaWiFi)
    {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"No WIFI available!", @"AlertView")
                                                            message:NSLocalizedString(@"You have no wifi connection available. Please connect to a WIFI network.", @"AlertView")
                                                           delegate:self
                                                  cancelButtonTitle:NSLocalizedString(@"Cancel", @"AlertView")
                                                  otherButtonTitles:NSLocalizedString(@"Open settings", @"AlertView"), nil];
        [alertView show];
        return NO:
    }
    return YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    if([self checkForWIFIConnection]) {
        [self parseXML]; // ...if this is here, but if i remove this line i get the UIALert saying no INTERNET
    }

}

return the status (a BOOL) and check it before calling parseXML, as seen above.

OTHER TIPS

In your .h, change it to:

- (BOOL)checkForWIFIConnection;

And in your .m, change it to:

- (BOOL)checkForWIFIConnection

Then also in the .m file, at the end of the method, add:

return netStatus == ReachableViaWiFi;

Then you can set up your if statement in the viewDidLoad method.

Simple. Have your checkForWIFIConnection method return a result:

- (BOOL)checkForWIFIConnection {
    Reachability* wifiReach = [Reachability reachabilityForLocalWiFi];
    NetworkStatus netStatus = [wifiReach currentReachabilityStatus];
    if (netStatus!=ReachableViaWiFi)
    {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"No WIFI available!", @"AlertView")
                                                            message:NSLocalizedString(@"You have no wifi connection available. Please connect to a WIFI network.", @"AlertView")
                                                           delegate:self
                                                  cancelButtonTitle:NSLocalizedString(@"Cancel", @"AlertView")
                                                  otherButtonTitles:NSLocalizedString(@"Open settings", @"AlertView"), nil];
        [alertView show];

        return NO;
    } else {
        return YES:
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];

    if ([self checkForWIFIConnection]) {
        [self parseXML];
    }

    ...
}

And you don't need to declare checkForWIFIConnection in the .h file. It's a private method not needed by other classes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top