iOS常用系统信息获取方法
一、手机电量获取,方法二需要导入头文件#import<objc/runtime.h>
方法一、获取电池电量(一般用百分数表示,大家自行处理就好)
1 -(CGFloat)getBatteryQuantity 2 { 3 return [[UIDevice currentDevice] batteryLevel]; 4 }
方法二、
1 - (int)getCurrentBatteryLevel 2 { 3 UIApplication *app = [UIApplication sharedApplication]; 4 if (app.applicationState == UIApplicationStateActive|| 5 app.applicationState==UIApplicationStateInactive) 6 { 7 Ivar ivar= class_getInstanceVariable([app class],"_statusBar"); 8 id status = object_getIvar(app, ivar); 9 for (id aview in [status subviews]) 10 { 11 int batteryLevel = 0; 12 for (id bview in [aview subviews]) 13 { 14 if ([NSStringFromClass([bview class]) caseInsensitiveCompare:@"UIStatusBarBatteryItemView"] 15 == NSOrderedSame&&[[[UIDevice currentDevice] systemVersion] floatValue] >=6.0) 16 { 17 18 Ivar ivar = class_getInstanceVariable([bview class],"_capacity"); 19 if(ivar) 20 { 21 batteryLevel = ((int (*)(id, Ivar))object_getIvar)(bview, ivar); 22 //这种方式也可以 23 /*ptrdiff_t offset = ivar_getOffset(ivar); 24 unsigned char *stuffBytes = (unsigned char *)(__bridge void *)bview; 25 batteryLevel = * ((int *)(stuffBytes + offset));*/ 26 if (batteryLevel > 0 && batteryLevel <= 100){ 27 return batteryLevel; 28 } 29 else{ 30 return 0; 31 } 32 } 33 } 34 } 35 } 36 } 37 return 0; 38 }
二、获取手机电池状态(UIDeviceBatteryState为枚举类型)
1 // UIDeviceBatteryStateUnknown:无法取得充电状态情况 2 // UIDeviceBatteryStateUnplugged:非充电状态 3 // UIDeviceBatteryStateCharging:充电状态 4 // UIDeviceBatteryStateFull:充满状态(连接充电器充满状态) 5 6 -(UIDeviceBatteryState)getBatteryStauts 7 { 8 UIDevice *device = [UIDevice currentDevice]; 9 device.batteryMonitoringEnabled = YES; 10 return device.batteryState; 11 }
三、获取手机总运行内存大小
1 //3.获取总内存大小 2 -(NSString *)getTotalMemorySize 3 { 4 return [self fileSizeToString:[NSProcessInfo processInfo].physicalMemory]; 5 } 6 7 -(NSString *)fileSizeToString:(unsigned long long)fileSize 8 { 9 NSInteger KB = 1024; 10 NSInteger MB = KB*KB; 11 NSInteger GB = MB*KB; 12 13 if (fileSize < 10) { 14 return @"0 B"; 15 }else if (fileSize < KB) { 16 return @"< 1 KB"; 17 }else if (fileSize < MB) { 18 return [NSString stringWithFormat:@"%.1f KB",((CGFloat)fileSize)/KB]; 19 }else if (fileSize < GB) { 20 return [NSString stringWithFormat:@"%.1f MB",((CGFloat)fileSize)/MB]; 21 }else { 22 return [NSString stringWithFormat:@"%.1f GB",((CGFloat)fileSize)/GB]; 23 } 24 }
四、获取当前剩余内存和已用内存,需要添加头文件#import <sys/sysctl.h> 和#import <mach/mach.h>
1 // 获取当前设备可用内存(单位:MB) 2 - (double)availableMemory 3 { 4 vm_statistics_data_t vmStats; 5 mach_msg_type_number_t infoCount = HOST_VM_INFO_COUNT; 6 kern_return_t kernReturn = host_statistics(mach_host_self(), 7 HOST_VM_INFO, 8 (host_info_t)&vmStats, 9 &infoCount); 10 11 if (kernReturn != KERN_SUCCESS) { 12 return NSNotFound; 13 } 14 15 return ((vm_page_size *vmStats.free_count) / 1024.0) / 1024.0; 16 } 17 18 // 获取当前任务所占用的内存(单位:MB) 19 - (double)usedMemory 20 { 21 task_basic_info_data_t taskInfo; 22 mach_msg_type_number_t infoCount = TASK_BASIC_INFO_COUNT; 23 kern_return_t kernReturn = task_info(mach_task_self(), 24 TASK_BASIC_INFO, 25 (task_info_t)&taskInfo, 26 &infoCount); 27 28 if (kernReturn != KERN_SUCCESS 29 ) { 30 return NSNotFound; 31 } 32 33 return taskInfo.resident_size / 1024.0 / 1024.0; 34 }
五、获取手机型号,需要添加头文件#import<sys/utsname.h>
1 + (NSString *)getCurrentiPhoneType 2 { 3 struct utsname systemInfo; 4 uname(&systemInfo); 5 NSString *platform = [NSString stringWithCString:systemInfo.machine encoding:NSASCIIStringEncoding]; 6 7 if ([platform isEqualToString:@"iPhone1,1"]) return @"iPhone 2G"; 8 9 if ([platform isEqualToString:@"iPhone1,2"]) return @"iPhone 3G"; 10 11 if ([platform isEqualToString:@"iPhone2,1"]) return @"iPhone 3GS"; 12 13 if ([platform isEqualToString:@"iPhone3,1"]) return @"iPhone 4"; 14 15 if ([platform isEqualToString:@"iPhone3,2"]) return @"iPhone 4"; 16 17 if ([platform isEqualToString:@"iPhone3,3"]) return @"iPhone 4"; 18 19 if ([platform isEqualToString:@"iPhone4,1"]) return @"iPhone 4S"; 20 21 if ([platform isEqualToString:@"iPhone5,1"]) return @"iPhone 5"; 22 23 if ([platform isEqualToString:@"iPhone5,2"]) return @"iPhone 5"; 24 25 if ([platform isEqualToString:@"iPhone5,3"]) return @"iPhone 5c"; 26 27 if ([platform isEqualToString:@"iPhone5,4"]) return @"iPhone 5c"; 28 29 if ([platform isEqualToString:@"iPhone6,1"]) return @"iPhone 5s"; 30 31 if ([platform isEqualToString:@"iPhone6,2"]) return @"iPhone 5s"; 32 33 if ([platform isEqualToString:@"iPhone7,1"]) return @"iPhone 6 Plus"; 34 35 if ([platform isEqualToString:@"iPhone7,2"]) return @"iPhone 6"; 36 37 if ([platform isEqualToString:@"iPhone8,1"]) return @"iPhone 6s"; 38 39 if ([platform isEqualToString:@"iPhone8,2"]) return @"iPhone 6s Plus"; 40 41 if([platform isEqualToString:@"iPhone8,4"]) return@"iPhone SE"; 42 43 if([platform isEqualToString:@"iPhone9,1"]) return@"iPhone 7"; 44 45 if([platform isEqualToString:@"iPhone9,2"]) return@"iPhone 7 Plus"; 46 47 if([platform isEqualToString:@"iPhone10,1"]) return@"iPhone 8"; 48 49 if([platform isEqualToString:@"iPhone10,4"]) return@"iPhone 8"; 50 51 if([platform isEqualToString:@"iPhone10,2"]) return@"iPhone 8 Plus"; 52 53 if([platform isEqualToString:@"iPhone10,5"]) return@"iPhone 8 Plus"; 54 55 if([platform isEqualToString:@"iPhone10,3"]) return@"iPhone X"; 56 57 if([platform isEqualToString:@"iPhone10,6"]) return@"iPhone X"; 58 59 60 if([platform isEqualToString:@"iPod1,1"]) return@"iPod Touch 1G"; 61 62 if([platform isEqualToString:@"iPod2,1"]) return@"iPod Touch 2G"; 63 64 if([platform isEqualToString:@"iPod3,1"]) return@"iPod Touch 3G"; 65 66 if([platform isEqualToString:@"iPod4,1"]) return@"iPod Touch 4G"; 67 68 if([platform isEqualToString:@"iPod5,1"]) return@"iPod Touch 5G"; 69 70 if([platform isEqualToString:@"iPad1,1"]) return@"iPad 1G"; 71 72 if([platform isEqualToString:@"iPad2,1"]) return@"iPad 2"; 73 74 if([platform isEqualToString:@"iPad2,2"]) return@"iPad 2"; 75 76 if([platform isEqualToString:@"iPad2,3"]) return@"iPad 2"; 77 78 if([platform isEqualToString:@"iPad2,4"]) return@"iPad 2"; 79 80 if([platform isEqualToString:@"iPad2,5"]) return@"iPad Mini 1G"; 81 82 if([platform isEqualToString:@"iPad2,6"]) return@"iPad Mini 1G"; 83 84 if([platform isEqualToString:@"iPad2,7"]) return@"iPad Mini 1G"; 85 86 if([platform isEqualToString:@"iPad3,1"]) return@"iPad 3"; 87 88 if([platform isEqualToString:@"iPad3,2"]) return@"iPad 3"; 89 90 if([platform isEqualToString:@"iPad3,3"]) return@"iPad 3"; 91 92 if([platform isEqualToString:@"iPad3,4"]) return@"iPad 4"; 93 94 if([platform isEqualToString:@"iPad3,5"]) return@"iPad 4"; 95 96 if([platform isEqualToString:@"iPad3,6"]) return@"iPad 4"; 97 98 if([platform isEqualToString:@"iPad4,1"]) return@"iPad Air"; 99 100 if([platform isEqualToString:@"iPad4,2"]) return@"iPad Air"; 101 102 if([platform isEqualToString:@"iPad4,3"]) return@"iPad Air"; 103 104 if([platform isEqualToString:@"iPad4,4"]) return@"iPad Mini 2G"; 105 106 if([platform isEqualToString:@"iPad4,5"]) return@"iPad Mini 2G"; 107 108 if([platform isEqualToString:@"iPad4,6"]) return@"iPad Mini 2G"; 109 110 if([platform isEqualToString:@"iPad4,7"]) return@"iPad Mini 3"; 111 112 if([platform isEqualToString:@"iPad4,8"]) return@"iPad Mini 3"; 113 114 if([platform isEqualToString:@"iPad4,9"]) return@"iPad Mini 3"; 115 116 if([platform isEqualToString:@"iPad5,1"]) return@"iPad Mini 4"; 117 118 if([platform isEqualToString:@"iPad5,2"]) return@"iPad Mini 4"; 119 120 if([platform isEqualToString:@"iPad5,3"]) return@"iPad Air 2"; 121 122 if([platform isEqualToString:@"iPad5,4"]) return@"iPad Air 2"; 123 124 if([platform isEqualToString:@"iPad6,3"]) return@"iPad Pro 9.7"; 125 126 if([platform isEqualToString:@"iPad6,4"]) return@"iPad Pro 9.7"; 127 128 if([platform isEqualToString:@"iPad6,7"]) return@"iPad Pro 12.9"; 129 130 if([platform isEqualToString:@"iPad6,8"]) return@"iPad Pro 12.9"; 131 132 if([platform isEqualToString:@"i386"]) return@"iPhone Simulator"; 133 134 if([platform isEqualToString:@"x86_64"]) return@"iPhone Simulator"; 135 136 return platform; 137 }
六、IP地址获取,只适用于WiFi网络需要添加以下头文件和宏定义
1 #import <ifaddrs.h> //ip 2 #import <arpa/inet.h> 3 #import <net/if.h> 4 #import <__clang_cuda_runtime_wrapper.h> 5 6 #define IOS_CELLULAR @"pdp_ip0" 7 #define IOS_WIFI @"en0" 8 #define IOS_VPN @"utun0" 9 #define IP_ADDR_IPv4 @"ipv4" 10 #define IP_ADDR_IPv6 @"ipv6"
方法一、
1 - (NSString *)deviceIPAdress { 2 NSString *address = @"an error occurred when obtaining ip address"; 3 struct ifaddrs *interfaces = NULL; 4 struct ifaddrs *temp_addr = NULL; 5 int success = 0; 6 7 success = getifaddrs(&interfaces); 8 if (success == 0) // 0 表示获取成功 9 { 10 temp_addr = interfaces; 11 while (temp_addr != NULL) { 12 if( temp_addr->ifa_addr->sa_family == AF_INET) { 13 // Check if interface is en0 which is the wifi connection on the iPhone 14 if ([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) { 15 // Get NSString from C String 16 address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)]; 17 } 18 } 19 20 temp_addr = temp_addr->ifa_next; 21 } 22 } 23 freeifaddrs(interfaces); 24 return address; 25 }
方法二、获取设备当前网络IP地址 (WiFi 手机流量通用)
1 //获取IPV4地址 2 + (NSString *)getDevicesIPV4Address 3 { 4 return [self getDevicesIPAddress:YES]; 5 } 6 7 //获取IPV6地址 8 + (NSString *)getDevicesIPV6Address 9 { 10 return [self getDevicesIPAddress:NO]; 11 }
1 + (NSString *)getDevicesIPAddress:(BOOL)preferIPv4 2 { 3 NSArray *searchArray = preferIPv4 ? @[IOS_VPN @"/" IP_ADDR_IPv4, IOS_VPN @"/" IP_ADDR_IPv6, IOS_WIFI @"/" IP_ADDR_IPv4, IOS_WIFI @"/" IP_ADDR_IPv6, IOS_CELLULAR @"/" IP_ADDR_IPv4, IOS_CELLULAR @"/" IP_ADDR_IPv6 ] : @[ IOS_VPN @"/" IP_ADDR_IPv6, IOS_VPN @"/" IP_ADDR_IPv4, IOS_WIFI @"/" IP_ADDR_IPv6, IOS_WIFI @"/" IP_ADDR_IPv4, IOS_CELLULAR @"/" IP_ADDR_IPv6, IOS_CELLULAR @"/" IP_ADDR_IPv4 ] ; 4 NSDictionary *addresses = [self getIPAddresses]; 5 __block NSString *address; 6 [searchArray enumerateObjectsUsingBlock:^(NSString *key, NSUInteger idx, BOOL *stop) 7 { 8 address = addresses[key]; 9 //筛选出IP地址格式 10 if([self isValidatIP:address isIpV4:preferIPv4]) *stop = YES; 11 } ]; 12 return address ? address : @"0.0.0.0"; 13 } 14 15 16 + (BOOL)isValidatIP:(NSString *)ipAddress isIpV4:(BOOL)ipv4 { 17 if (ipAddress.length == 0) { 18 return NO; 19 } 20 NSString *urlRegEx; 21 if (ipv4) 22 { 23 urlRegEx = @"^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." 24 "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." 25 "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." 26 "([01]?\\d\\d?|2[0-4]\\d|25[0-5])$"; 27 } 28 else 29 { 30 urlRegEx = @"^\\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:)))(%.+)?\\s*$"; 31 } 32 33 NSError *error; 34 NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:urlRegEx options:0 error:&error]; 35 36 if (regex != nil) { 37 NSTextCheckingResult *firstMatch=[regex firstMatchInString:ipAddress options:0 range:NSMakeRange(0, [ipAddress length])]; 38 if (firstMatch) { 39 // NSRange resultRange = [firstMatch rangeAtIndex:0]; 40 // NSString *result=[ipAddress substringWithRange:resultRange]; 41 return YES; 42 } 43 } 44 return NO; 45 } 46 47 + (NSDictionary *)getIPAddresses 48 { 49 NSMutableDictionary *addresses = [NSMutableDictionary dictionaryWithCapacity:8]; 50 51 // retrieve the current interfaces - returns 0 on success 52 struct ifaddrs *interfaces; 53 if(!getifaddrs(&interfaces)) { 54 // Loop through linked list of interfaces 55 struct ifaddrs *interface; 56 for(interface=interfaces; interface; interface=interface->ifa_next) { 57 if(!(interface->ifa_flags & IFF_UP) /* || (interface->ifa_flags & IFF_LOOPBACK) */ ) { 58 continue; // deeply nested code harder to read 59 } 60 const struct sockaddr_in *addr = (const struct sockaddr_in*)interface->ifa_addr; 61 char addrBuf[ MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) ]; 62 if(addr && (addr->sin_family==AF_INET || addr->sin_family==AF_INET6)) { 63 NSString *name = [NSString stringWithUTF8String:interface->ifa_name]; 64 NSString *type; 65 if(addr->sin_family == AF_INET) { 66 if(inet_ntop(AF_INET, &addr->sin_addr, addrBuf, INET_ADDRSTRLEN)) { 67 type = IP_ADDR_IPv4; 68 } 69 } else { 70 const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6*)interface->ifa_addr; 71 if(inet_ntop(AF_INET6, &addr6->sin6_addr, addrBuf, INET6_ADDRSTRLEN)) { 72 type = IP_ADDR_IPv6; 73 } 74 } 75 if(type) { 76 NSString *key = [NSString stringWithFormat:@"%@/%@", name, type]; 77 addresses[key] = [NSString stringWithUTF8String:addrBuf]; 78 } 79 } 80 } 81 // Free memory 82 freeifaddrs(interfaces); 83 } 84 return [addresses count] ? addresses : nil; 85 }
还需要用到另一个辅助类IPAddressConfig
IPAddressConfig.h
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <unistd.h> 5 #include <sys/ioctl.h> 6 #include <sys/types.h> 7 #include <sys/socket.h> 8 #include <netinet/in.h> 9 #include <netdb.h> 10 #include <arpa/inet.h> 11 #include <sys/sockio.h> 12 #include <net/if.h> 13 #include <errno.h> 14 #include <net/if_dl.h> 15 #include <net/ethernet.h> 16 #include <ifaddrs.h> 17 #include <arpa/inet.h> 18 19 #define BUFFERSIZE 4000 20 #define MAXADDRS 32 21 #define min(a,b) ((a) < (b) ? (a) : (b)) 22 #define max(a,b) ((a) > (b) ? (a) : (b)) 23 24 @interface IPAddressConfig : NSObject 25 26 // extern 27 extern char *if_names[MAXADDRS]; 28 extern char *ip_names[MAXADDRS]; 29 extern char *hw_addrs[MAXADDRS]; 30 extern unsigned long ip_addrs[MAXADDRS]; 31 32 // Function prototypes 33 void InitAddresses(void); 34 void FreeAddresses(void); 35 void GetIPAddresses(void); 36 void GetHWAddresses(void); 37 38 @end
IPAddressConfig.m
1 @implementation IPAddressConfig 2 3 char *if_names[MAXADDRS]; 4 char *ip_names[MAXADDRS]; 5 char *hw_addrs[MAXADDRS]; 6 unsigned long ip_addrs[MAXADDRS]; 7 8 static int nextAddr = 0; 9 10 void InitAddresses() 11 { 12 int i; 13 for (i=0; i<MAXADDRS; ++i) 14 { 15 if_names[i] = ip_names[i] = hw_addrs[i] = NULL; 16 ip_addrs[i] = 0; 17 } 18 } 19 20 void FreeAddresses() 21 { 22 int i; 23 for (i=0; i<MAXADDRS; ++i) 24 { 25 if (if_names[i] != 0) free(if_names[i]); 26 if (ip_names[i] != 0) free(ip_names[i]); 27 if (hw_addrs[i] != 0) free(hw_addrs[i]); 28 ip_addrs[i] = 0; 29 } 30 InitAddresses(); 31 } 32 33 void GetIPAddresses() 34 { 35 int i, len, flags; 36 char buffer[BUFFERSIZE], *ptr, lastname[IFNAMSIZ], *cptr; 37 struct ifconf ifc; 38 struct ifreq *ifr, ifrcopy; 39 struct sockaddr_in *sin; 40 41 char temp[80]; 42 43 int sockfd; 44 45 for (i=0; i<MAXADDRS; ++i) 46 { 47 if_names[i] = ip_names[i] = NULL; 48 ip_addrs[i] = 0; 49 } 50 51 sockfd = socket(AF_INET, SOCK_DGRAM, 0); 52 if (sockfd < 0) 53 { 54 perror("socket failed"); 55 return; 56 } 57 58 ifc.ifc_len = BUFFERSIZE; 59 ifc.ifc_buf = buffer; 60 61 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) 62 { 63 perror("ioctl error"); 64 return; 65 } 66 67 lastname[0] = 0; 68 69 for (ptr = buffer; ptr < buffer + ifc.ifc_len; ) 70 { 71 ifr = (struct ifreq *)ptr; 72 len = max(sizeof(struct sockaddr), ifr->ifr_addr.sa_len); 73 ptr += sizeof(ifr->ifr_name) + len; // for next one in buffer 74 75 if (ifr->ifr_addr.sa_family != AF_INET) 76 { 77 continue; // ignore if not desired address family 78 } 79 80 if ((cptr = (char *)strchr(ifr->ifr_name, ':')) != NULL) 81 { 82 *cptr = 0; // replace colon will null 83 } 84 85 if (strncmp(lastname, ifr->ifr_name, IFNAMSIZ) == 0) 86 { 87 continue; /* already processed this interface */ 88 } 89 90 memcpy(lastname, ifr->ifr_name, IFNAMSIZ); 91 92 ifrcopy = *ifr; 93 ioctl(sockfd, SIOCGIFFLAGS, &ifrcopy); 94 flags = ifrcopy.ifr_flags; 95 if ((flags & IFF_UP) == 0) 96 { 97 continue; // ignore if interface not up 98 } 99 100 if_names[nextAddr] = (char *)malloc(strlen(ifr->ifr_name)+1); 101 if (if_names[nextAddr] == NULL) 102 { 103 return; 104 } 105 strcpy(if_names[nextAddr], ifr->ifr_name); 106 107 sin = (struct sockaddr_in *)&ifr->ifr_addr; 108 strcpy(temp, inet_ntoa(sin->sin_addr)); 109 110 ip_names[nextAddr] = (char *)malloc(strlen(temp)+1); 111 if (ip_names[nextAddr] == NULL) 112 { 113 return; 114 } 115 strcpy(ip_names[nextAddr], temp); 116 117 ip_addrs[nextAddr] = sin->sin_addr.s_addr; 118 119 ++nextAddr; 120 } 121 122 close(sockfd); 123 } 124 125 void GetHWAddresses() 126 { 127 struct ifconf ifc; 128 struct ifreq *ifr; 129 int i, sockfd; 130 char buffer[BUFFERSIZE], *cp, *cplim; 131 char temp[80]; 132 133 for (i=0; i<MAXADDRS; ++i) 134 { 135 hw_addrs[i] = NULL; 136 } 137 138 sockfd = socket(AF_INET, SOCK_DGRAM, 0); 139 if (sockfd < 0) 140 { 141 perror("socket failed"); 142 return; 143 } 144 145 ifc.ifc_len = BUFFERSIZE; 146 ifc.ifc_buf = buffer; 147 148 if (ioctl(sockfd, SIOCGIFCONF, (char *)&ifc) < 0) 149 { 150 perror("ioctl error"); 151 close(sockfd); 152 return; 153 } 154 155 ifr = ifc.ifc_req; 156 157 cplim = buffer + ifc.ifc_len; 158 159 for (cp=buffer; cp < cplim; ) 160 { 161 ifr = (struct ifreq *)cp; 162 if (ifr->ifr_addr.sa_family == AF_LINK) 163 { 164 struct sockaddr_dl *sdl = (struct sockaddr_dl *)&ifr->ifr_addr; 165 int a,b,c,d,e,f; 166 int i; 167 168 strcpy(temp, (char *)ether_ntoa((const struct ether_addr *)LLADDR(sdl))); 169 sscanf(temp, "%x:%x:%x:%x:%x:%x", &a, &b, &c, &d, &e, &f); 170 sprintf(temp, "%02X:%02X:%02X:%02X:%02X:%02X",a,b,c,d,e,f); 171 172 for (i=0; i<MAXADDRS; ++i) 173 { 174 if ((if_names[i] != NULL) && (strcmp(ifr->ifr_name, if_names[i]) == 0)) 175 { 176 if (hw_addrs[i] == NULL) 177 { 178 hw_addrs[i] = (char *)malloc(strlen(temp)+1); 179 strcpy(hw_addrs[i], temp); 180 break; 181 } 182 } 183 } 184 } 185 cp += sizeof(ifr->ifr_name) + max(sizeof(ifr->ifr_addr), ifr->ifr_addr.sa_len); 186 } 187 close(sockfd); 188 } 189 190 191 @end
七、获取手机当前连接的WiFi名称
1 - (NSString *)getWifiName 2 { 3 NSString *wifiName = nil; 4 5 CFArrayRef wifiInterfaces = CNCopySupportedInterfaces(); 6 if (!wifiInterfaces) { 7 return nil; 8 } 9 10 NSArray *interfaces = (__bridge NSArray *)wifiInterfaces; 11 12 for (NSString *interfaceName in interfaces) { 13 CFDictionaryRef dictRef = CNCopyCurrentNetworkInfo((__bridge CFStringRef)(interfaceName)); 14 15 if (dictRef) { 16 NSDictionary *networkInfo = (__bridge NSDictionary *)dictRef; 17 18 wifiName = [networkInfo objectForKey:(__bridge NSString *)kCNNetworkInfoKeySSID]; 19 20 CFRelease(dictRef); 21 } 22 } 23 24 CFRelease(wifiInterfaces); 25 return wifiName; 26 }