不断检测互联网连接

我想让我的应用程序自动检测互联网连接丢失。 所以即时通讯使用下面的代码。

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

但问题是,它不是连续检查互联网连接。 它只在应用程序激活时才检查。 如何在整个应用生命周期中连续检查互联网连接,并在互联网自动closures时发出警告?

在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]; } 

一旦你的应用程序启动,你可以触发一个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 } } 

谢谢!

计时器不是一个有效的方法来做到这一点,但你也可以使用计时器。

 - (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 } } 

最好的方法是使用Reachability代码。 在这里检查苹果示例代码。 这有很多方便的方法来检查互联网的可用性,WiFi / WAN连接检查等。

例如:-

 [[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"); } } 

你只能在后台检查这个东西

*audio – 应用程序在后台播放可听内容给用户。 (此内容包括使用AirPlay播放audio或video内容。)

*位置 – 应用程序让用户知道他们的位置,即使它在后台运行。

* voip – 该应用程序提供用户使用Internet连接拨打电话的function。

*报亭内容 – 该应用程序是一个报亭应用程序,在后台下载和处理杂志或报纸内容。

*外部附件 – 该应用程序与硬件附件,需要通过外部附件框架定期提供更新。

*蓝牙中央 – 该应用程序与蓝牙配件,需要通过核心蓝牙框架定期提供更新。

*蓝牙外设 – 该应用程序通过核心蓝牙框架支持外设模式下的蓝牙通信。

首先导入你的类: #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; } 

添加一个观察者。

 [[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; } 

您可以使用iOS的Reachability框架,并将其与NSTimer结合使用。