简体   繁体   English

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

[英]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. 我在我的iOS应用程序中使用了MMLANScanner库。 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. 它向我显示了连接到本地WiFi路由器的所有设备以及以前连接到本地WiFi路由器的设备。

How I can make decision whether the device IP that I am getting is presently connected to my Local WiFi Router? 我如何确定当前获取的设备IP是否已连接到本地WiFi路由器?

Using Apple's Reachability example here is some code to check the connectivity of a device. 使用Apple的Reachability示例,这里是一些代码来检查设备的连接性。

#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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在设备连接到网络(wifi / 3G)时检查互联网连接,但在使用Swift的iOS中没有互联网访问? - How to check for internet connectivity when device is connected to a network (wifi/3G) but there is no internet access in iOS using Swift? 电话的当前IP地址,无论是通过WiFi还是3G连接 - Current IP address of phone, whether connected via WiFi or 3G Swift - 获取设备的 WIFI IP 地址 - Swift - Get device's WIFI IP Address 如何从外壳获取已连接的iOS设备的WiFi IP? - How to get WiFi IP of connected iOS device from the shell? 使用 Swift 获取连接到我 iPhone 个人热点的设备的 IP 地址 - Get IP Address of Device connected to my iPhone's Personal Hotspot using Swift 如何知道IP地址是否在本地网络中,xcode - How to know if an IP address is in local network, xcode 检查设备是否连接到 VPN - Check whether device is connected to a VPN 如何使用正则表达式检查私有/本地IP地址 - How to check private/local IP address by using regular expression 检测wifi已启用(无论是否已连接) - Detect wifi enabled (regardless of whether it's connected) 当设备连接到某个热点并且该热点的移动数据已关闭时,如何检查网络可达性 - How to check network reachability when the device is connected to some hotspot and that hotspot's mobile data is off
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM