Tôi đã được thử nghiệm cách khác nhau để thực hiện khả năng biết nếu thiết bị có được internet sau khi ứng dụng nó là ở chế độ nền nên kiểm tra mã tôi đầu tiên là Apple mẫu reachability đang http://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.htmlDò tìm các reachability trong nền
Nhưng mã này không thông báo trạng thái internet khi Ứng dụng ở chế độ nền. Vì vậy, tôi cũng đã cố gắng mã folowing và nó hoạt động khi ứng dụng được khởi chạy từ trạng thái nền để foreground (giống như Apple mẫu mã reachability)
- (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;
}
}
}
Câu hỏi của tôi là:là gì cách để nhận được thông báo khi internet trạng thái thay đổi khi ứng dụng ở chế độ nền?
Xin chào, trước tiên bạn nên lưu ý rằng Apple rất nghiêm ngặt về loại ứng dụng nào sẽ có thể thực hiện thao tác nền. Xin hãy xem tài liệu này của Apple. https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html. ngoài ra, hãy cố gắng giải thích ứng dụng của bạn đang cố gắng làm gì. Có lẽ chúng tôi có thể cung cấp một cách tiếp cận tốt hơn! –