Ich habe verschiedene Art und Weise getestet, die Möglichkeit zu implementieren zu wissen, ob das Gerät Internet zurück, wenn die App es im Hintergrund ist so der erste Code-I-Test der Apple-Erreichbarkeitsbeispielcode war http://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.htmlDetect die Erreichbarkeit im Hintergrund
Aber dieser Code benachrichtigt Internet nicht, wenn die App im Hintergrund ist. Also habe ich versucht, auch den Folowing Code und es funktioniert, wenn App von Hintergrundzustand in dem Vordergrund (das gleiche wie Apple-Erreichbarkeitsbeispielcode) gestartet wird
- (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;
}
}
}
Meine Frage ist:Was die Art und Weise informiert werden, wenn Internet Status geändert, wenn die App im Hintergrund ist?
Hallo, zuerst sollten Sie beachten, dass Apple sehr streng darüber ist, welche Arten von Anwendungen im Hintergrund ausgeführt werden können. Bitte werfen Sie einen Blick auf dieses Dokument von Apple. https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html. Darüber hinaus versuchen Sie bitte zu erklären, was Ihre App versucht zu tun. Vielleicht können wir einen besseren Ansatz bieten! –