he estado probando manera diferente para poner en práctica la posibilidad de conocer si el dispositivo de conseguir Internet espalda cuando la aplicación es en el fondo lo que la primera prueba de código que era el código de Apple muestra de accesibilidad http://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.htmlDetectar si se pueden alcanzar en el fondo
Pero este código no notifica el estado de Internet cuando la aplicación está en segundo plano. Así que he intentado también el código folowing y que funcione cuando la aplicación se inicia desde el estado Antecedentes de primer plano (el mismo que Apple código de ejemplo de accesibilidad)
- (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;
}
}
}
Mi pregunta es:¿Cuál es el camino para ser notificado cuando internet estado cambiado cuando la aplicación está en segundo plano?
Hola, primero debe tener en cuenta que Apple es muy estricto sobre qué tipos de aplicaciones podrán realizar ejecuciones en segundo plano. Pls eche un vistazo a este documento de Apple. https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html. Además, los pls intentan explicar qué está intentando hacer tu aplicación. Tal vez podamos ofrecer un mejor enfoque! –