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
}
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.
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With