2012-10-01 31 views
19

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?

Trả lời

2

Tôi không tin rằng có cách nhận thông báo khả năng hiển thị trong khi bạn đang ở chế độ nền. Cách chính xác để xử lý việc này là kiểm tra khả năng truy cập trong ứng dụng - (void) applicationWillEnterForeground: (UIApplication *) của AppDelegate.

Sự kiện nền duy nhất mà ứng dụng nền phản ứng là nhận thông báo đẩy và đó là do hệ điều hành đánh thức họ làm như vậy và chỉ khi người dùng yêu cầu.

-2

Ứng dụng của bạn phải đa nhiệm (VoIP, LOCATION hoặc Âm thanh). Và theo cách này, bạn có thể bắt mạng thay đổi khi ứng dụng chạy ẩn.

+1

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! –

7

Sống bạn không thể thay đổi nếu kết nối mạng thay đổi, tốt nhất bạn có thể làm để làm cho nó hoạt động là sử dụng chế độ Background Fetch từ Capabilities. Firstable bạn cần phải kiểm tra hộp kiểm cho chế độ nền: enter image description here

Sau đó, bạn cần phải hỏi cho thời gian khoảng thường xuyên như bạn có thể càng sớm càng tốt vì vậy tôi đề nghị application:didFinishLaunchingWithOptions: và bạn cần phải đặt dòng này:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    [application setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum]; 
    return YES; 
} 

các UIApplicationBackgroundFetchIntervalMinimum nó càng nhiều càng tốt, nhưng nó không phải là con số chính xác giây giữa fetches từ các tài liệu:

các int nhỏ lấy được hỗ trợ bởi hệ thống.

Và sau đó khi nền lấy sẽ cháy bạn có thể kiểm tra trong AppDelegate với phương pháp:

-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{ 
    Reachability *reachability = [Reachability reachabilityForInternetConnection]; 
    [reachability startNotifier]; 
    NetworkStatus status = [reachability currentReachabilityStatus]; 

    switch (status) { 
     case NotReachable: { 
      NSLog(@"no internet connection"); 
      break; 
     } 
     case ReachableViaWiFi: { 
      NSLog(@"wifi"); 
      break; 
     } 
     case ReachableViaWWAN: { 
      NSLog(@"cellurar"); 
      break; 
     } 
    } 
    completionHandler(YES); 
} 

Tất cả điều này sẽ làm việc trong iOS 7.0 hoặc cao hơn.

+0

Tôi đã triển khai như đã giải thích .. nhưng vẫn thực hiệnFetchWithCompletionHandler không thực hiện cuộc gọi và không thể kiểm tra khả năng hiển thị. – CKT