简体   繁体   中英

How to check whether a device in local WiFi network is connected or not using it's IP address?

I am using MMLANScanner library into my iOS application. It's showing me all the devices that are connected to my Local WiFi Router along with the devices that was previously connected to my Local WiFi Router.

How I can make decision whether the device IP that I am getting is presently connected to my Local WiFi Router?

Using Apple's Reachability example here is some code to check the connectivity of a device.

#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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM