Ho testato modo diverso per implementare la possibilità di sapere se il dispositivo di avere internet indietro quando l'applicazione è in background in modo che il primo test codice che è stato il codice di esempio raggiungibilità di Apple http://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.htmlRilevare la raggiungibilità in background
Ma questo codice non notifica lo stato di Internet quando l'App è in background. Così ho provato anche il codice folowing e funziona quando App viene lanciato dallo stato di sfondo a primo piano (lo stesso di Apple codice di esempio raggiungibilità)
- (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;
}
}
}
La mia domanda è:Qual è il modo per essere avvisato quando internet stato modificato quando l'app è in background?
Ciao, per prima cosa dovresti notare che Apple è molto severa su quali tipi di app saranno in grado di eseguire esecuzioni in background. Dai un'occhiata a questo documento di Apple. https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html. inoltre, pls prova a spiegare cosa sta cercando di fare la tua app. Forse possiamo offrire un approccio migliore! –