ios - 持续监听 Internet 可达性?

标签 ios objective-c networking reachability

我了解如何在我的应用程序中测试互联网可达性,但我需要做的是不断监听应用程序范围内的可达性。因此,如果应用程序中任何地方的任何一点连接状态发生变化,我都可以使用react。

我怎样才能实现这样的目标?

最佳答案

您需要为可达性更改通知添加一个观察者:

首先在你的类中导入:#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])
    {
        //notificationLabel.text = @"Notification Says Reachable"
        status = YES;
        NSLog(@"NetWork is Available");
    }
    else
    {
        status = NO;
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"You are not connected to the internet" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
    }
    return status;
}

关于ios - 持续监听 Internet 可达性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19051169/

相关文章:

objective-c - NSLocalizedString 格式

objective-c - NSDateFormatter 从字符串返回错误的日期

objective-c - NSFileManager 中的错误(实例方法正在遍历符号链接(symbolic link))

ios - 如何强制 UICollectionView 滚动到特定行并加载该行的单元格,同时隐藏 Collection View ?

ios - 值类型和 init 的问题

ios - 刷新 UIViewController

java - 数据报包数量

networking - 公共(public)IP范围可以用作私有(private)IP吗?

http - 如何做 HTTP 等同于多播

ios - Apple APNS 推送通知有多安全?