【问题标题】:Notification when WiFi connection availableWiFi 连接可用时的通知
【发布时间】:2015-05-07 18:46:41
【问题描述】:

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

我使用了来自 Reachability 的以下代码:

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: 方法没有正确和准确地调用。所以,请指导我解决这个问题。

任何解决问题的帮助都必须感谢。

【问题讨论】:

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


    【解决方案1】:

    希望对您的问题有所帮助。

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

    【讨论】:

    • 谢谢。让我实现它并在这里做评论。
    【解决方案2】:

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

    // Use below to any method
    // check for internet connection
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];
    
    internetReachability = [Reachability reachabilityForInternetConnection];
    [internetReachability startNotifier];
    
    
    -(void) checkNetworkStatus:(NSNotification *)notice {
        // called after network status changes
        NetworkStatus internetStatus = [internetReachability currentReachabilityStatus];
    
        switch (internetStatus) {
            case NotReachable: {
                NSLog(@"The internet is down.");
                break;
            }
    
            case ReachableViaWiFi: {
                NSLog(@"The internet is working via WIFI.");
                //Remove Observer
                [[NSNotificationCenter defaultCenter] removeObserver:self name:kReachabilityChangedNotification object:nil];
                [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];
    
                //Write your code                
                break;
            }
    
            case ReachableViaWWAN: {
                NSLog(@"The internet is working via WWAN.");
                break;
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-21
      • 1970-01-01
      • 2013-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-21
      相关资源
      最近更新 更多