0

Is there any event or any other way to detect when a iPad or iPhone goes out of a specific WiFi range. I searched for this and could only find Reachability class and that also i will have to check continuously in the background for the connectivity, which is not preferred in my case. Any help will be appreciated...

spider1983
  • 1,098
  • 1
  • 7
  • 14

2 Answers2

2

This is the sample code you can use to find wifi/internet conncetivity

.h file

@class Reachability;

@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
    Reachability* wifiReach;
}

.m file

**

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        self.viewController = [[HPViewController alloc] initWithNibName:@"HPViewController_iPhone" bundle:nil];
    } else {
        self.viewController = [[HPViewController alloc] initWithNibName:@"HPViewController_iPad" bundle:nil];
    }

    _navCon =[[UINavigationController alloc] initWithRootViewController:self.viewController];
    self.window.rootViewController = _navCon;

    // Observe the kNetworkReachabilityChangedNotification. When that notification is posted, the
    // method "reachabilityChanged" will be called.
    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil];
    //Change the host name here to change the server your monitoring
    wifiReach = [[Reachability reachabilityForLocalWiFi] retain];
    [wifiReach startNotifier];
    [self showAlertOfInternetConncetion: wifiReach];

    return YES;
}
//Called by Reachability whenever status changes.
- (void) reachabilityChanged: (NSNotification* )note
{
    Reachability* curReach = [note object];
    NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
    [self showAlertOfInternetConncetion: curReach];
}
- (void)showAlertOfInternetConncetion : (Reachability*) curReach
{
    NetworkStatus netStatus = [curReach currentReachabilityStatus];
    switch (netStatus)
    {
        case NotReachable:
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Internet connection" message:@"Your internet connection has stopped working. Please check it." delegate:self cancelButtonTitle:nil otherButtonTitles:@"Exit App", nil];
            [alert show];
            [alert release];
        }
        case ReachableViaWWAN:
        {
            NSLog(@"ReachableVia WWAN");
        }
        case ReachableViaWiFi:
        {
            NSLog(@"ReachableVia WiFi");
        }
    }   
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        exit(0);
    }
}

**

hp iOS Coder
  • 3,230
  • 2
  • 23
  • 39
  • @spider1983 Have you tried this and found that it works? If not, please don't accept answers until you are sure they answer your question. Also, this is just the Reachability code that you said you already found and it wouldn't work for you..... – Nick Bull Dec 19 '12 at 11:35
  • @NickBull oh ok sure i was exited knowing the way itself ...i'll correct that... i am sorry for that. – spider1983 Dec 19 '12 at 11:43
  • @NickBull I put this code in the **Appdelegate.m** & whenever I reached out of WiFi range I found it working by showing me the `UIAlertView` I wrote in the code – hp iOS Coder Dec 19 '12 at 12:10
  • @hpiOSCoder I'm not doubting that this is a correct answer - I'm sure it is! What I was saying was that the OP had marked it as correct before checking himself that it was correct. I was also just pointing out to him that it was what he had already dismissed as not being appropriate! There's nothing wrong with your answer! – Nick Bull Dec 19 '12 at 13:26
  • 1
    @NickBull learned things from you that without checking don't mark as accepted and also it was not like i didn't wanted to use Reachability class it was just that i didn't wanted to engage my thread, and also i was not aware of the notifier in reachability class...thanks anyways for all the suggestions. – spider1983 Dec 19 '12 at 15:08
  • @spider1983 Thanks for accepting. Glad it helped you. I agree your point, that you was not aware of notification so was looking for alternative to `Reachability`. Happy coding!! Cheers – hp iOS Coder Dec 20 '12 at 04:50
0

Use Apple's SCNetworkReachability class. Read the SCNetworkReachability documentation. Test using:

(ReachabilityInst.currentReachabilityStatus().value == ReachableViaWiFi.value)
Alvin George
  • 14,148
  • 92
  • 64