2016-08-01 158 views
3

我希望我的應用能夠自動檢測互聯網連接丟失。所以即時通訊使用下面的代碼。不斷檢測互聯網連接

- (void)applicationDidBecomeActive:(UIApplication *)application { 

    Reachability *networkReachability = [Reachability reachabilityForInternetConnection]; 
    NetworkStatus networkStatus = [networkReachability currentReachabilityStatus]; 
    if (networkStatus == NotReachable) { 
     [Settings hideSpinner]; 
     //Show no internet connectivity dialog. 
    } else { 
    } 
} 

但問題是,它不是連續檢查互聯網連接。 只有當應用程序變爲活動狀態時纔會檢查。如何在整個應用生命週期中連續檢查互聯網連接,並在互聯網自動關閉時發出警告?

+0

Reachability提供了一個在狀態改變時執行的回調。使用它。 – Avi

回答

2

在Reachability方法中添加這樣的obeserver。

1)[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged :) name:kReachabilityChangedNotification object:nil];

它會自動調用當你的應用程序打開/在後臺模式,它調用reachabilityChanged。

2)[[NSNotificationCenter defaultCenter] postNotificationName:@「ChangeInternetConnection」object:nil];

ChangeInternetConnection你有觀察者添加到您的的ViewController拿到狀態時,互聯網改變其狀態。

- (void) checkInternetConnetion { 

     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil]; 

     //NSString *remoteHostName = @"www.apple.com"; 


    self.internetReachability = [Reachability reachabilityForInternetConnection]; 
     [self.internetReachability startNotifier]; 
     [self updateInterfaceWithReachability:self.internetReachability]; 
    } 


    #pragma mark - Reachability Methods 

    - (void)updateInterfaceWithReachability:(Reachability *)reachability { 
    if (reachability == self.internetReachability) { 
      [self checkStatus:reachability]; 
     } 

     if (reachability == self.wifiReachability) { 
      [self checkStatus:reachability]; 
     } 
    } 


    -(void)checkStatus :(Reachability *)reachability { 

     NetworkStatus netStatus = [reachability currentReachabilityStatus]; 
     BOOL connectionRequired = [reachability connectionRequired]; 
     NSString* statusString = @""; 

     switch (netStatus) { 

      case NotReachable: { 

       self.isInternetOn = FALSE; 

       break; 
      } 

      case ReachableViaWWAN: { 
       self.isInternetOn = TRUE; 

       break; 
      } 
      case ReachableViaWiFi: { 
       self.isInternetOn = TRUE; 

       break; 
      } 
     } 



     [[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeInternetConnection" object:nil]; 

     } 

    - (void) reachabilityChanged:(NSNotification *)note { 

     Reachability* curReach = [note object]; 
     NSParameterAssert([curReach isKindOfClass:[Reachability class]]); 
     [self updateInterfaceWithReachability:curReach]; 
    } 
+0

做這件事,它不是加載網站檢查互聯網是否可用的最佳做法,因爲它會降低應用程序的性能 – Learner

+1

謝謝...順便說一句,我沒有在任何代碼中使用該變量。 – Bhoomi

+0

好的謝謝...... – Learner

1

首先導入您的類:#import "Reachability.h"

然後執行類似下面的樣子:

添加可達性變化通知觀察者

[[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(reachabilityChanged:) 
               name:kReachabilityChangedNotification object:nil]; 


-(BOOL)reachabilityChanged:(NSNotification*)note 
{ 
    BOOL status =YES; 

    NSLog(@"reachabilityChanged"); 

    Reachability * reach = [note object]; 

    if([reach isReachable]) 
    { 
     status = YES; 

     NSLog(@"your network is Available"); 
    } 

    else 

    { 
     status = NO; 

//Do something here 

    } 
    return status; 
} 
+0

我如何能夠在後臺連續檢測到這種情況? – Learner

1

計時器沒有這樣做,但你的有效途徑也可以使用定時器。

- (void)applicationDidBecomeActive:(UIApplication *)application 
{ 
     NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0f 
                target:self 
               selector:@selector(handleConnectivity) 
               userInfo:nil 
               repeats:YES]; 
} 
-(void)handleConnectivity 
{ 
    Reachability *networkReachability = [Reachability reachabilityForInternetConnection]; 
    NetworkStatus networkStatus = [networkReachability currentReachabilityStatus]; 
    if (networkStatus == NotReachable) 
    { 
     //No internet connectivity - perform required action 
    } 
    else 
    { 
     //Internet connectivity is valid 
    } 
} 

最好的方法是使用可達性代碼。 Check here for apple sample code.,有很多的方便的方法來檢查網絡可用性,WIFI /廣域網連接性檢查等。

例如: -

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkChanged:) name:kReachabilityChangedNotification object:nil]; 

reachability = [Reachability reachabilityForInternetConnection]; 
[reachability startNotifier]; 

- (void)networkChanged:(NSNotification *)notification 
{ 

    NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus]; 

    if(remoteHostStatus == NotReachable) { NSLog(@"not reachable");} 
    else if (remoteHostStatus == ReachableViaWiFiNetwork) { NSLog(@"wifi"); } 
    else if (remoteHostStatus == ReachableViaCarrierDataNetwork) { NSLog(@"carrier"); } 
} 

您只能檢查這個東西在後臺

*音頻 - 應用程序在後臺播放可聽內容給用戶。 (此內容包括使用AirPlay的流式音頻或視頻內容 。)

*位置 - 即使應用程序在後臺運行時,也會向用戶通知其位置。

* voip-該應用程序提供了用戶使用Internet連接撥打電話的功能。

*報亭內容 - 該應用程序是一個報亭應用程序,可在後臺下載和處理雜誌或報紙內容。

*外部附件 - 該應用程序與需要通過外部附件框架定期傳遞更新的硬件附件配合工作。

*藍牙中央 - 該應用程序與需要通過核心藍牙 框架定期提供更新的藍牙配件一起使用。

*藍牙外設 - 該應用程序支持通過核心藍牙框架在外設模式下的藍牙通信。

+0

可以在後臺連續運行嗎? – Learner

+0

你只能在背景 – PinkeshGjr

3

一旦你的應用程序已經啓動,你可以觸發一個的NSTimer做同樣的:

- (void)applicationDidBecomeActive:(UIApplication *)application { 
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0f 
               target:self 
              selector:@selector(checkForConnectivity) 
              userInfo:nil 
              repeats:YES]; 
} 

-(void)checkForConnectivity { 
     Reachability *networkReachability = [Reachability reachabilityForInternetConnection]; 
     NetworkStatus networkStatus = [networkReachability currentReachabilityStatus]; 
     if (networkStatus == NotReachable) { 
       //No internet connectivity - perform required action 
     } 
     else { 
       //Internet connectivity is valid 
     } 
} 

謝謝!

1

添加一個觀察者。

[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(reachabilityChanged:) 
              name:kReachabilityChangedNotification 
              object:nil]; 

-(BOOL)reachabilityChanged:(NSNotification*)note 
{ 
    BOOL status =YES; 
    NSLog(@"reachability is changed"); 

    Reachability * reach = [note object]; 

    if([reach isReachable]) 
    { 
     status = YES; 
     NSLog(@"NetWork is Available. Please go ahead"); 
    } 
    else 
    { 
     status = NO; 
     NSLog(@"NetWork is not Available. Please check your connection."); 
    } 
    return status; 
} 
-1

您可以利用iOS的Reachability框架並將其與NSTimer結合使用。