I have been testing different way to implement the possibility to know if the device get internet back when the app it is in background so the first code I test was the Apple reachability sample code http://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.html
But this code doesn´t notify internet state when the App it´s in background. So I tried also the folowing code and it work when App is launched from Background state to foreground (same as Apple reachability sample code)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// check for internet connection
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(checkNetworkStatus:)
name:kReachabilityChangedNotification object:nil];
// Set up Reachability
internetReachable = [[Reachability reachabilityForInternetConnection] retain];
[internetReachable startNotifier];
...
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
// check for internet connection
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(checkNetworkStatus:)
name:kReachabilityChangedNotification object:nil];
// Set up Reachability
internetReachable = [[Reachability reachabilityForInternetConnection] retain];
[internetReachable startNotifier];
}
- (void)checkNetworkStatus:(NSNotification *)notice {
// called after network status changes
NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
switch (internetStatus)
{
case NotReachable:
{
NSLog(@"The internet is down.");
break;
}
case ReachableViaWiFi:
{
NSLog(@"The internet is working via WIFI");
//Alert sound in Background when App have internet again
UILocalNotification *notification = [[[UILocalNotification alloc] init] autorelease];
if (notification) {
[notification setFireDate:[NSDate date]];
[notification setTimeZone:[NSTimeZone defaultTimeZone]];
[notification setRepeatInterval:0];
[notification setSoundName:@"alarmsound.caf"];
[notification setAlertBody:@"Send notification internet back"];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
break;
}
case ReachableViaWWAN:
{
NSLog(@"The internet is working via WWAN!");
//Alert sound in Background when App have internet again
UILocalNotification *notification = [[[UILocalNotification alloc] init] autorelease];
if (notification) {
[notification setFireDate:[NSDate date]];
[notification setTimeZone:[NSTimeZone defaultTimeZone]];
[notification setRepeatInterval:0];
[notification setSoundName:@"alarmsound.caf"];
[notification setAlertBody:@"Send notification internet back"];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
break;
}
}
}
My question is : What is the way to get notified when internet state changed when the app is in Background ?
I don't believe there is a way to receive reachability notifications while you're in the background. The correct way to handle this would be to check for reachability in AppDelegate's - (void)applicationWillEnterForeground:(UIApplication *)application.
The only background event that background apps react to is receipt of push notifications, and that is because the OS wakes them up to do so, and then only when the user requests it.
You app should be multitasking (VoIP, LOCATION or Audio). And in this way, you can catch the network changing when the app in the background.
Live you cannot change if the network connection changed, the very best you can do to make it work is to use![enter image description here](https://i.stack.imgur.com/BSVI6.png)
Background Fetch
mode fromCapabilities
. Firstable you need to check the checkbox for background mode:Then you need to ask for time interval as often as you can the sooner the better so i suggest
application:didFinishLaunchingWithOptions:
and you need to put this line:The
UIApplicationBackgroundFetchIntervalMinimum
it's as often as possible, but it's not exact number of seconds between fetches from the docs:And then when background fetch will fire you can check in
AppDelegate
with method:All of this will work in iOS 7.0 or higher.