1

I need to check if the user is connected to wifi or not. If the user is connected to mobile internet i want a alert message to say something like "for best performance, you should be connected to wifi" How can i do this in Objective-C?

Freddy
  • 810
  • 8
  • 19
  • Look into the Reachability classes http://stackoverflow.com/questions/1083701/how-to-check-for-an-active-internet-connection-on-iphone-sdk/7934636#7934636 – thelaws Jun 16 '14 at 20:58

1 Answers1

2

Oops, sorry I didn't realize you wanted Wifi. For that case, reachability is your friend.

- (void)checkForWIFIConnection
{
Reachability* wifiReach = [Reachability reachabilityForInternetConnection];

NetworkStatus netStatus = [wifiReach currentReachabilityStatus];

if (netStatus != ReachableViaWiFi)// ReachableViaWWAN == 3G, ReachableViaWiFi == WIFI
{
    NSString *message  = [[NSString alloc] initWithFormat:NSLocalizedString(@"InitialNoWifiMessage", nil)];

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"InitialNoWifiTitle", nil)
                                                        message:message
                                                       delegate:self
                                              cancelButtonTitle:NSLocalizedString(@"InitialNoWifiAccept", nil)
                                              otherButtonTitles:nil];
    [alertView show];
}
else
{
    // connected to Wifi
}
}
TooManyEduardos
  • 3,516
  • 5
  • 28
  • 61
  • This will only determine whether the network is connected, not if it's a wifi or 3G/LTE connection. – thelaws Jun 16 '14 at 20:58
  • I modified now to check for Wifi connectivity – TooManyEduardos Jun 16 '14 at 21:02
  • Do I need to import something? I get error message that Reachability is not a valid code. – Freddy Jun 16 '14 at 21:10
  • Yeah you need to add the Reachability.h and Reachibility.m to your project (you can get them from apple here:https://developer.apple.com/Library/ios/samplecode/Reachability/Listings/Reachability_Reachability_m.html#//apple_ref/doc/uid/DTS40007324-Reachability_Reachability_m-DontLinkElementID_8) and then in the class that you'll use the code do #import "Reachability.h" – TooManyEduardos Jun 16 '14 at 21:13
  • ohh thanks, Found that out. – Freddy Jun 16 '14 at 21:19
  • mark the answer as correct, if it works – TooManyEduardos Jun 16 '14 at 21:20