Currently i am using the class by apple reachability.m/.h and it works, except it notifies me for any change, where as i would like to only notify the user if the network is not reachable. Currently if i have a internet connection and then loose the network it tells me. However when you reconnect to the network it also tells me, which i do not want. I want it to only tell me when there is a loss/no network.
I believe it has something to do with the call:
- (void)viewWillAppear:(BOOL)animated
{
// check for internet connection
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(checkNetworkStatus:)
name:kReachabilityChangedNotification
object:nil];
internetReachable = [[Reachability
reachabilityForInternetConnection] retain];
[internetReachable startNotifier];
// check if a pathway to a random host exists
hostReachable = [[Reachability reachabilityWithHostName:
@"www.google.ca"] retain];
[hostReachable startNotifier];
// now patiently wait for the notification
}
when calling -[NSNotificationCenter addObserver:selector:name:object:]
, does the name have any other function then being literally a name? this is my first time using NSNotificationCenter so i am not well versed in this matter.
EDIT:
Here is my checkNetworkStatus function: (The problem is i am getting "NotReachable" as the network connection is coming back and NSAlert goes off multiple times)
- (void) checkNetworkStatus:(NSNotification *)notice
{
// called after network status changes
NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
switch (internetStatus)
{
case NotReachable:
{
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Network Failed" message:@"Please check your connection and try again." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil ];
[alert show];
NSLog(@"The internet is down.");
break;
}
case ReachableViaWiFi:
{
NSLog(@"The internet is working via WIFI.");
break;
}
case ReachableViaWWAN:
{
NSLog(@"The internet is working via WWAN.");
break;
}
}
NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
switch (hostStatus)
{
case NotReachable:
{
NSLog(@"A gateway to the host server is down.");
break;
}
case ReachableViaWiFi:
{
NSLog(@"A gateway to the host server is working via WIFI.");
break;
}
case ReachableViaWWAN:
{
NSLog(@"A gateway to the host server is working via WWAN.");
break;
}
}
}
Enabling Reachability On iPhone 13 Pro Max Once enabled on an iPhone without a Home button, such as the iPhone 13 Pro Max, the user will need to swipe down on the bottom edge of the display to activate the one-handed mode. On an iPhone with a Home button, double-tapping the button will activate one-handed mode.
With Android 12 or higher, you can turn on a one-handed mode that lowers the top half of the screen, similar to the Reachability feature on an iPhone. To enable this on most Android phones, go to Settings > System > Gestures > One-handed Mode and turn on the switch for Use one-handed mode.
Notification On Reachability. Through this service you will be notified if you try to call any number and was unreachable or switched off by receiving an SMS indicates that the number become available now.
When you use iPhone with one hand in Portrait orientation, you can use Reachability to lower the top half of the screen so it's within easy reach of your thumb. (Not supported on iPhone SE (1st generation).) Go to Settings > Accessibility > Touch, then turn on Reachability.
Reachability will send a notification when the status has changed, but what you do with that notification is entirely up to you. If you don't want to tell the user that the network is back, you don't have to.
The "name" parameter in the NSNotificationCenter method indicates what notification you are subscribing to. When an object posts a notification, it does so with a particular name.
If you replace the www.hostname.com with just an IP address, it will only alert once instead of multiple times.
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