2016-08-01 155 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结合使用。