mac地址获取:(iOS7已废用)
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#import <sys/sysctl.h> #import <net/if.h> #import <net/if_dl.h>
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 + (NSString *)getMacAddress 2 { 3 int mgmtInfoBase[6]; 4 char *msgBuffer = NULL; 5 size_t length; 6 unsigned char macAddress[6]; 7 struct if_msghdr *interfaceMsgStruct; 8 struct sockaddr_dl *socketStruct; 9 NSString *errorFlag = NULL; 10 11 // Setup the management Information Base (mib) 12 mgmtInfoBase[0] = CTL_NET; // Request network subsystem 13 mgmtInfoBase[1] = AF_ROUTE; // Routing table info 14 mgmtInfoBase[2] = 0; 15 mgmtInfoBase[3] = AF_LINK; // Request link layer information 16 mgmtInfoBase[4] = NET_RT_IFLIST; // Request all configured interfaces 17 18 // With all configured interfaces requested, get handle index 19 if ((mgmtInfoBase[5] = if_nametoindex("en0")) == 0) 20 errorFlag = @"if_nametoindex failure"; 21 else 22 { 23 // Get the size of the data available (store in len) 24 if (sysctl(mgmtInfoBase, 6, NULL, &length, NULL, 0) < 0) 25 errorFlag = @"sysctl mgmtInfoBase failure"; 26 else 27 { 28 // Alloc memory based on above call 29 if ((msgBuffer = malloc(length)) == NULL) 30 errorFlag = @"buffer allocation failure"; 31 else 32 { 33 // Get system information, store in buffer 34 if (sysctl(mgmtInfoBase, 6, msgBuffer, &length, NULL, 0) < 0) 35 errorFlag = @"sysctl msgBuffer failure"; 36 } 37 } 38 } 39 // Befor going any further... 40 if (errorFlag != NULL) 41 { 42 NSLog(@"Error: %@", errorFlag); 43 return errorFlag; 44 } 45 46 // Map msgbuffer to interface message structure 47 interfaceMsgStruct = (struct if_msghdr *) msgBuffer; 48 49 // Map to link-level socket structure 50 socketStruct = (struct sockaddr_dl *) (interfaceMsgStruct + 1); 51 52 // Copy link layer address data in socket structure to an array 53 memcpy(&macAddress, socketStruct->sdl_data + socketStruct->sdl_nlen, 6); 54 55 // Read from char array into a string object, into traditional Mac address format 56 NSString *macAddressString = [NSString stringWithFormat:@"%02x:%02x:%02x:%02x:%02x:%02x", 57 macAddress[0], macAddress[1], macAddress[2], 58 macAddress[3], macAddress[4], macAddress[5]]; 59 NSLog(@"Mac Address: %@", macAddressString); 60 61 // Release the buffer memory 62 free(msgBuffer); 63 64 return macAddressString; 65 }
IP地址获取:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include <ifaddrs.h>
#include <arpa/inet.h>
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 + (NSString *)getIPAddress 2 { 3 NSString *address = @"error"; 4 struct ifaddrs *interfaces = NULL; 5 struct ifaddrs *temp_addr = NULL; 6 int success = 0; 7 // retrieve the current interfaces - returns 0 on success 8 success = getifaddrs(&interfaces); 9 if (success == 0) 10 { 11 // Loop through linked list of interfaces 12 temp_addr = interfaces; 13 while(temp_addr != NULL) 14 { 15 if(temp_addr->ifa_addr->sa_family == AF_INET) 16 { 17 // Check if interface is en0 which is the wifi connection on the iPhone 18 if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) 19 { 20 // Get NSString from C String 21 address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)]; 22 } 23 } 24 temp_addr = temp_addr->ifa_next; 25 } 26 } 27 // Free memory 28 freeifaddrs(interfaces); 29 return address; 30 }