如何检查本地WiFi网络中的设备是否已连接或使用其IP地址?

问题描述:

我在我的iOS应用程序中使用MMLANScanner库。它向我展示了连接到本地WiFi路由器的所有设备以及之前连接到本地WiFi路由器的设备。如何检查本地WiFi网络中的设备是否已连接或使用其IP地址?

我如何判断我所获得的设备IP是否连接到本地WiFi路由器?

+0

感谢您的回答。但是,对于每个我得到的IP地址,可达性始终显示为可REACHABLE。 – appleBoy21

利用苹果公司的Reachability example这里是一些代码来检查设备的连通性。

#import "Reachability.h" 

@interface ViewController : UIViewController 
{ 
    BOOL isInternetConnectionAvailable; 
} 
@end 
@implementation ViewController 
- (void)viewDidLoad { 
    [super viewDidLoad]; 
//--Reachability-- 
    /* 
    Observe the kNetworkReachabilityChangedNotification. When that notification is posted, the method reachabilityChanged will be called. 
    */ 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil]; 

    self.internetReachability = [Reachability reachabilityForInternetConnection]; 
    [self.internetReachability startNotifier]; 
    [self updateInterfaceWithReachability:self.internetReachability]; 
    //--+--// 


} 
#pragma mark Reachability 
/*! 
* Called by Reachability whenever status changes. 
*/ 
- (void) reachabilityChanged:(NSNotification *)note 
{ 
    Reachability* curReach = [note object]; 
    NSParameterAssert([curReach isKindOfClass:[Reachability class]]); 
    [self updateInterfaceWithReachability:curReach]; 
} 


- (void)updateInterfaceWithReachability:(Reachability *)reachability 
{ 

    if (reachability == self.internetReachability) 
    { 
     NetworkStatus netStatus = [reachability currentReachabilityStatus]; 
     BOOL connectionRequired = [reachability connectionRequired]; 
     NSString* statusString = @""; 

     // 
     UIAlertController * connectivityAlert = [UIAlertController alertControllerWithTitle:@"Connectivity" message:[NSString stringWithFormat:@"Status:%@",statusString] preferredStyle:UIAlertControllerStyleAlert]; 
     UIAlertAction * ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]; 
     [connectivityAlert addAction:ok]; 
     // 

     switch (netStatus) 
     { 
      case NotReachable:  { 
       statusString = NSLocalizedString(@"Access Not Available", @"Text field text for access is not available"); 
       /* 
       Minor interface detail- connectionRequired may return YES even when the host is unreachable. We cover that up here... 
       */ 
       connectionRequired = NO; 

       connectivityAlert.message = [NSString stringWithFormat:@"Status:%@",statusString]; 
       [self presentViewController:connectivityAlert animated:YES completion:nil]; 
       isInternetConnectionAvailable = NO; 
       break; 
      } 

      case ReachableViaWWAN:  { 
       statusString = NSLocalizedString(@"Reachable WWAN", @""); 

       connectivityAlert.message = [NSString stringWithFormat:@"Status:%@",statusString]; 

       isInternetConnectionAvailable = YES; 
       break; 
      } 
      case ReachableViaWiFi:  { 
       statusString= NSLocalizedString(@"Reachable WiFi", @""); 

       connectivityAlert.message = [NSString stringWithFormat:@"Status:%@",statusString]; 

       isInternetConnectionAvailable = YES; 
       break; 
      } 
     } 

     if (connectionRequired) 
     { 
      NSString *connectionRequiredFormatString = NSLocalizedString(@"%@, Connection Required", @"Concatenation of status string with connection requirement"); 
      statusString= [NSString stringWithFormat:connectionRequiredFormatString, statusString]; 

      connectivityAlert.message = [NSString stringWithFormat:@"Status:%@",statusString]; 
      isInternetConnectionAvailable = NO; 
      [self presentViewController:connectivityAlert animated:YES completion:nil]; 
     } 
    } 

} 
@end 
+0

可达性类还显示脱机设备可达到的状态。我已经尝试过可达性。 – appleBoy21