12

嗨,我想在用户在我的应用程序中获得网络连接时捕获我已经添加了 apples Reachability 类,下面是我在 appDelegate 类 didFinishLaunchingWithOptions 方法中使用的片段,

Reachability* reachability = [Reachability reachabilityForInternetConnection];
        [reachability startNotifier];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];

我的reachabilityChanged选择器方法如下

- (void)reachabilityChanged:(NSNotification*)notification
{
    Reachability* reachability = notification.object;
    if(reachability.currentReachabilityStatus == NotReachable)
        NSLog(@"Internet off");
    else
        NSLog(@"Internet on");
}

但是在这里,当我关闭飞行模式以及在手机中获得网络连接时,我没有收到任何通知。

我错过了什么吗?

4

5 回答 5

14

我在 appdelegate 中使用变量将当前网络状态存储为布尔值

@property (nonatomic, assign) BOOL hasInet;

.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [self setUpRechability];
}


-(void)setUpRechability
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNetworkChange:) name:kReachabilityChangedNotification object:nil];

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

    NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus];

    if          (remoteHostStatus == NotReachable)      {NSLog(@"no");      self.hasInet-=NO;   }
    else if     (remoteHostStatus == ReachableViaWiFi)  {NSLog(@"wifi");    self.hasInet-=YES;  }
    else if     (remoteHostStatus == ReachableViaWWAN)  {NSLog(@"cell");    self.hasInet-=YES;  }

}

- (void) handleNetworkChange:(NSNotification *)notice
{
    NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus];

    if          (remoteHostStatus == NotReachable)      {NSLog(@"no");      self.hasInet-=NO;   }
    else if     (remoteHostStatus == ReachableViaWiFi)  {NSLog(@"wifi");    self.hasInet-=YES;  }
    else if     (remoteHostStatus == ReachableViaWWAN)  {NSLog(@"cell");    self.hasInet-=YES;  }

//    if (self.hasInet) {
//        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Net avail" message:@"" delegate:self cancelButtonTitle:OK_EN otherButtonTitles:nil, nil];
//        [alert show];
//    }
}
于 2013-07-23T09:25:48.477 回答
6

也许你应该在 startnotifier 之前添加观察者

[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(handleNetworkChange:) name: kReachabilityChangedNotification object: nil];
reachability = [Reachability reachabilityForInternetConnection];
[reachability startNotifier];
于 2013-07-23T09:09:19.793 回答
0

每当连接状态发生变化时,您可以使用可达性包装器来获取基于块的回调。

GitHub:UHB连接管理器

如果您使用的是 cocoapods。

pod 'UHBConnectivityManager'
于 2016-08-25T12:51:53.197 回答
0

最简单的方法:

reach = [Reachability reachabilityWithHostName:@"www.google.com"];
 reach.reachableBlock = ^(Reachability *reach){
    NSLog(@"Reachable");
};

reach.unreachableBlock = ^(Reachability *reach){
    NSLog(@"Unreachable");
};
[reach startNotifier];

每次网络发生变化时,都会调用相应的块。

于 2017-02-18T18:26:45.433 回答
0

将可达性作为您的属性而不是局部变量。它会起作用的。

于 2017-03-09T08:02:03.893 回答