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...
Asked
Active
Viewed 495 times
0
-
well if the way you should do it is "not preferred", what else is preferred? – jimpic Dec 19 '12 at 10:40
-
checking continuously for wifi will slow down my app as i have already too many background event going on.If i could get an event or so will be best thing. – spider1983 Dec 19 '12 at 10:42
-
1check http://stackoverflow.com/questions/11083369/ios-wifi-notification-api – jimpic Dec 19 '12 at 10:44
-
2You should re-read the Reachability example and documentation. Your code gets notified when there is a change to the connection - you don't have to continuously poll it – Nick Bull Dec 19 '12 at 10:46
-
1check this answer http://stackoverflow.com/questions/339089/can-the-iphone-sdk-obtain-the-wi-fi-ssid-currently-connected-to – Paras Joshi Dec 19 '12 at 10:46
-
@jimpic thanks alot guys for such a quick suggestion will give it a try. – spider1983 Dec 19 '12 at 10:49
-
@NickBull thanks alot guys for such a quick suggestion will give it a try. – spider1983 Dec 19 '12 at 10:50
-
1@ParasJoshi thanks alot for such a quick suggestion will give it a try. – spider1983 Dec 19 '12 at 10:50
2 Answers
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