WiFi连接可用时通知

时间:2015-05-07 18:46:42

标签: ios objective-c notifications wifi internet-connection

当设备有WiFi连接可用或设备通过WiFi连接时,我需要通知。只有当WiFi可用时我才需要做一些事情。

我使用了以下来自可达性的代码:

BOOL status=true;

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];

internetReachability = [Reachability reachabilityForInternetConnection];
[internetReachability startNotifier];

NetworkStatus internetNetworkStatus = [internetReachability currentReachabilityStatus];
status = (internetNetworkStatus == ReachableViaWiFi);

checkNetworkStatus:方法未正确准确地调用。所以,请指导我解决这个问题。

任何帮助解决问题都必须表示赞赏。

2 个答案:

答案 0 :(得分:2)

我希望它可以帮助你解决问题。

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



self.internetConnectionReach = [Reachability reachabilityForInternetConnection];

self.internetConnectionReach.reachableBlock = ^(Reachability * reachability)
{

   NSLog(@"%@", reachability.currentReachabilityString);

    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
  dispatch_async(dispatch_get_main_queue(), ^{

      // Do stuff here when WIFI is availble

     }];
};

self.internetConnectionReach.unreachableBlock = ^(Reachability * reachability)
{
  NSLog(@"%@", reachability.currentReachabilityString);
    dispatch_async(dispatch_get_main_queue(), ^{
      [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        // do some stuff here when WIFI not present 
    }];
};    
[self.internetConnectionReach startNotifier];        

}

-(void)reachabilityChanged:(NSNotification*)note
{

     Reachability * reach = [note object];
     if (reach == self.localWiFiReach)
     {
     if([reach isReachable])
     {
     NSString * temp = [NSString stringWithFormat:@"LocalWIFI Notification Says Reachable(%@)", reach.currentReachabilityString];
     NSLog(@"%@", temp);
      }
     else
     {
     NSString * temp = [NSString stringWithFormat:@"LocalWIFI Notification Says Unreachable(%@)", reach.currentReachabilityString];
    NSLog(@"%@", temp);
     }
     }

}

答案 1 :(得分:0)

以下方法可以帮助我解决问题:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
  <li>one</li>
  <li>two</li>
  <li>three</li>
  <li>four</li>
  <li>five</li>
</ul>