5

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

我将如何实现这样的目标?

4

1 回答 1

5

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

首先在您的课程中导入:#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;
}
于 2013-09-27T12:55:46.907 回答