美文网首页
iOS开发中WiFi相关功能

iOS开发中WiFi相关功能

作者: 树根曰 | 来源:发表于2016-12-28 21:57 被阅读0次

    在开发中, 需要获取 WiFi 信息的一些方法, 在此记录下来:

    #import <NetworkExtension/NEHotspotHelper.h>
    #import <SystemConfiguration/CaptiveNetwork.h>
    
    #import <arpa/inet.h>
    #import <netinet/in.h>
    #import <ifaddrs.h>
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        
        NSDictionary * WiFiInfo = [self fetchSSIDInfo];
        NSLog(@"WiFiInfo == %@",WiFiInfo);
        
        //WiFi 名称
        NSString *WiFiName = [self fetchWiFiName];
        NSLog(@"WiFiName == %@",WiFiName);
        
        //
        NSString *Ip = [self getLocalIPAddressForCurrentWiFi];
        NSLog(@"Ip == %@",Ip);
        
        NSMutableDictionary *data = [self getLocalInfoForCurrentWiFi];
        NSLog(@"广播地址、子网掩码、端口 == %@",data);
        
    }
    
    //获取WiFi 信息,返回的字典中包含了WiFi的名称、路由器的Mac地址、还有一个Data(转换成字符串打印出来是wifi名称)
    - (NSDictionary *)fetchSSIDInfo {
        
        NSArray *ifs = (__bridge_transfer NSArray *)CNCopySupportedInterfaces();
        
        if (!ifs) {
            
            return nil;
            
        }
        
        NSDictionary *info = nil;
        
        for (NSString *ifnam in ifs) {
            
            info = (__bridge_transfer NSDictionary *)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam);
            
            if (info && [info count]) { break; }
            
        }
        
        return info;
        
    }
    
    //获取 WiFi 名称
    - (NSString *)fetchWiFiName {
        
        NSArray *ifs = (__bridge_transfer NSArray *)CNCopySupportedInterfaces();
        
        if (!ifs) {
            
            return nil;
            
        }
        
        NSString *WiFiName = nil;
        
        for (NSString *ifnam in ifs) {
            
            NSDictionary *info = (__bridge_transfer NSDictionary *)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam);
            
            if (info && [info count]) {
                
                // 这里其实对应的有三个key:kCNNetworkInfoKeySSID、kCNNetworkInfoKeyBSSID、kCNNetworkInfoKeySSIDData,
                
                // 不过它们都是CFStringRef类型的
                
                WiFiName = [info objectForKey:(__bridge NSString *)kCNNetworkInfoKeySSID];
                
                //            WiFiName = [info objectForKey:@"SSID"];
                
                break;
                
            }
            
        }
        
        return WiFiName;
    }
    
    //获取本机在WiFi环境下的IP地址
    - (NSString *)getLocalIPAddressForCurrentWiFi
    {
        NSString *address = nil;
        struct ifaddrs *interfaces = NULL;
        struct ifaddrs *temp_addr = NULL;
        int success = 0;
        
        // retrieve the current interfaces - returns 0 on success
        success = getifaddrs(&interfaces);
        if (success == 0) {
            // Loop through linked list of interfaces
            temp_addr = interfaces;
            while(temp_addr != NULL) {
                if(temp_addr->ifa_addr->sa_family == AF_INET) {
                    // Check if interface is en0 which is the wifi connection on the iPhone
                    if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) {
                        address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
                        return address;
                    }
                }
                temp_addr = temp_addr->ifa_next;
            }
            freeifaddrs(interfaces);
        }
        return nil;
    }
    
    //获取本机在WiFi环境下的IP地址, 广播地址、子网掩码、端口等,组装成一个字典。
    - (NSMutableDictionary *)getLocalInfoForCurrentWiFi {
        
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        struct ifaddrs *interfaces = NULL;
        struct ifaddrs *temp_addr = NULL;
        int success = 0;
        
        // retrieve the current interfaces - returns 0 on success
        success = getifaddrs(&interfaces);
        if (success == 0) {
            // Loop through linked list of interfaces
            temp_addr = interfaces;
            //*/
            while(temp_addr != NULL) {
                if(temp_addr->ifa_addr->sa_family == AF_INET) {
                    // Check if interface is en0 which is the wifi connection on the iPhone
                    if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) {
                        
                        //----192.168.1.255 广播地址
                        NSString *broadcast = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_dstaddr)->sin_addr)];
                        
                        if (broadcast) {
                            [dict setObject:broadcast forKey:@"broadcast"];
                        }
                        NSLog(@"broadcast address--%@",broadcast);
                        //--192.168.1.106 本机地址
                        NSString *localIp = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
                        if (localIp) {
                            [dict setObject:localIp forKey:@"localIp"];
                        }
                        NSLog(@"local device ip--%@",localIp);
                        //--255.255.255.0 子网掩码地址
                        NSString *netmask = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_netmask)->sin_addr)];
                        if (netmask) {
                            [dict setObject:netmask forKey:@"netmask"];
                        }
                        NSLog(@"netmask--%@",netmask);
                        //--en0 端口地址
                        NSString *interface = [NSString stringWithFormat:@"%d",temp_addr->ifa_flags];
                        if (interface) {
                            [dict setObject:interface forKey:@"interface"];
                        }
                        NSLog(@"interface--%@",interface);
                        return dict;
                    }
                }
                
                temp_addr = temp_addr->ifa_next;
            }
        }
        
        // Free memory
        freeifaddrs(interfaces);
        return dict;
    }
    

    相关文章

      网友评论

          本文标题:iOS开发中WiFi相关功能

          本文链接:https://www.haomeiwen.com/subject/wxdkvttx.html