Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if there is an active internet connection iPhone situation

I would like to check to see if the user has an active internet connection. This is how I have implemented it. It seems to work fine but the problem is it ALWAYS shows that there is no connection on my iPhone simulator (uialert comes up) even when my wifi is turned on or off. Does any know what I am doing wrong? Thanks for your help!

Reachability *r= [Reachability reachabilityWithHostName:@"http://www.google.com"];
    NetworkStatus internetStatus= [r currentReachabilityStatus];

 if ((internetStatus != ReachableViaWiFi) && (internetStatus != ReachableViaWWAN))
    {

        UIAlertView *alert= [[UIAlertView alloc] initWithTitle:@"No internet" message:@"No internet connection found. Please try again later"
                                                      delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];
    }

 else {

 // execute code in app

 }
like image 646
Teddy13 Avatar asked Oct 02 '11 20:10

Teddy13


People also ask

How do I check my iPhone Internet connection?

Go to Settings > Wi-Fi and make sure that Wi-Fi is on. Tap the name of your Wi-Fi network to join. A blue checkmark beside a network name means that you're connected.

Why does my iPhone say Internet connection appears to be offline?

After activating your iPhone, you may get the following error message: “The internet connection appears to be offline”. This will typically occur when your phone or SIM card has not yet been activated.


1 Answers

This is how I have done it in my apps:

Reachability *reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus internetStatus = [reachability currentReachabilityStatus];

if(internetStatus == NotReachable) {
    UIAlertView *errorView;

    errorView = [[UIAlertView alloc]
                 initWithTitle: NSLocalizedString(@"Network error", @"Network error")
                 message: NSLocalizedString(@"No internet connection found, this application requires an internet connection to gather the data required.", @"Network error")
                 delegate: self
                 cancelButtonTitle: NSLocalizedString(@"Close", @"Network error") otherButtonTitles: nil];

    [errorView show];
    [errorView autorelease];
}

What is does is that it checks for an internetconnection, not if it can reach a domain. If no internet connection (wifi or celluar) it will show an UIAlertView message (localized).

like image 79
Paul Peelen Avatar answered Oct 17 '22 20:10

Paul Peelen