码迷,mamicode.com
首页 > 其他好文 > 详细

wifi,网关相关标识的获取

时间:2016-07-25 16:11:48      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:

获取WIFI的相关信息

 1 - (void)getWifiInfo
 2 {
 3     NSArray *ifs = (__bridge_transfer NSArray *)CNCopySupportedInterfaces();
 4     if (!ifs) {
 5         return ;
 6     }
 7     NSDictionary *info = nil;
 8     for (NSString *ifnam in ifs) {
 9         info = (__bridge_transfer NSDictionary *)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam);
10         if (info && [info count]) {
11             break;
12         }
13     }
14     NSLog(@"WIFI名称 %@    MAC地址 %@",info[@"SSID"],info[@"BSSID"]);
15 }

获取WIFI网关以及子网掩码,端口等信息

 1 - (NSMutableDictionary *)getLocalInfoForCurrentWiFi {
 2     NSMutableDictionary *dict = [NSMutableDictionary dictionary];
 3     struct ifaddrs *interfaces = NULL;
 4     struct ifaddrs *temp_addr = NULL;
 5     int success = 0;
 6     // retrieve the current interfaces - returns 0 on success
 7     success = getifaddrs(&interfaces);
 8     if (success == 0) {
 9         // Loop through linked list of interfaces
10         temp_addr = interfaces;
11         //*/
12         while(temp_addr != NULL) {
13             if(temp_addr->ifa_addr->sa_family == AF_INET) {
14                 // Check if interface is en0 which is the wifi connection on the iPhone
15                 if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) {
16                     //----192.168.1.255 广播地址
17                     NSString *broadcast = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_dstaddr)->sin_addr)];
18                     if (broadcast) {
19                         [dict setObject:broadcast forKey:@"broadcast"];
20                     }
21                     NSLog(@"broadcast address--%@",broadcast);
22                     //--192.168.1.106 本机地址
23                     NSString *localIp = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
24                     if (localIp) {
25                         [dict setObject:localIp forKey:@"localIp"];
26                     }
27                     NSLog(@"local device ip--%@",localIp);
28                     //--255.255.255.0 子网掩码地址
29                     NSString *netmask = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_netmask)->sin_addr)];
30                     if (netmask) {
31                         [dict setObject:netmask forKey:@"netmask"];
32                     }
33                     NSLog(@"netmask--%@",netmask);
34                     //--en0 端口地址
35                     NSString *interface = [NSString stringWithUTF8String:temp_addr->ifa_name];
36                     if (interface) {
37                         [dict setObject:interface forKey:@"interface"];
38                     }
39                     NSLog(@"interface--%@",interface);
40                     return dict;
41                 }
42             }
43             temp_addr = temp_addr->ifa_next;
44         }
45     }
46     // Free memory
47     freeifaddrs(interfaces);
48     return dict;
49 }

 

wifi,网关相关标识的获取

标签:

原文地址:http://www.cnblogs.com/zhanghuanan/p/5703644.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!