2015-05-07 74 views
0

當設備具有WiFi連接可用或設備通過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

我希望它能幫助你解決問題。

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

} 
+0

感謝。讓我來實施它,並會在這裏發表評論。 –

+0

你的問題解決了嗎? –

0

下面的方式是幫助我解決我的問題:

// 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; 
     } 
    } 
}