在项目中遇到需要获取设备名字、IP地址和MAC地址信息,之前用NSHost,结果发现不是很稳定,就放弃了,另外想出了几个方法,下面记录一下:
ViewController.h
//
// ViewController.h
// DeviceMACAddress
//
// Created by Chen Ling on 18/7/2018.
// Copyright © 2018 Chen Ling. All rights reserved.
//
#import
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *deviceNameLable;
@property (weak, nonatomic) IBOutlet UILabel *deviceIPAddressLable;
@property (weak, nonatomic) IBOutlet UILabel *deviceMACAddressLable;
@end
ViewController.m
//
// ViewController.m
// DeviceMACAddress
//
// Created by Chen Ling on 18/7/2018.
// Copyright © 2018 Chen Ling. All rights reserved.
//
#import "ViewController.h"
#import
#import
#import
#import
#import
#import
#import
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_deviceNameLable.text = [self getDeviceName];
_deviceIPAddressLable.text = [self getDeviceIPAddress];
_deviceMACAddressLable.text = [self getDeviceMacAddress];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Gets the device name
- (NSString *)getDeviceName {
#if (TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE)
return [NSString stringWithFormat:@"%@",[UIDevice currentDevice].name];
#else // iOS平台
return [NSString stringWithFormat:@"%@",[NSHost currentHost].localizedName];
#endif // macOS平台
}
#pragma mark - Get the device IP address
- (NSString *)getDeviceIPAddress {
NSString *address = @"an error occurred when obtaining ip address";
struct ifaddrs *interfaces = NULL;
struct ifaddrs *temp_addr = NULL;
int success = 0;
success = getifaddrs(&interfaces);
if (success == 0) { // 0 表示获取成功
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"]) {
// Get NSString from C String
struct sockaddr_in *sockaddr = (struct sockaddr_in *)temp_addr->ifa_addr;
address = [NSString stringWithUTF8String:inet_ntoa(sockaddr->sin_addr)];
}
}
temp_addr = temp_addr->ifa_next;
}
}
freeifaddrs(interfaces);
NSLog(@"手机的IP是:%@", address);
return address;
}
#pragma mark - Get the device MAC address
- (NSString *) getDeviceMacAddress
{
int mib[6];
size_t len;
char *buf;
unsigned char *ptr;
struct if_msghdr *ifm;
struct sockaddr_dl *sdl;
mib[0] = CTL_NET;
mib[1] = AF_ROUTE;
mib[2] = 0;
mib[3] = AF_LINK;
mib[4] = NET_RT_IFLIST;
if ((mib[5] = if_nametoindex("en0")) == 0) {
printf("Error: if_nametoindex error/n");
return NULL;
}
if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) {
printf("Error: sysctl, take 1/n");
return NULL;
}
if ((buf = malloc(len)) == NULL) {
printf("Could not allocate memory. error!/n");
return NULL;
}
if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
printf("Error: sysctl, take 2");
return NULL;
}
ifm = (struct if_msghdr *)buf;
sdl = (struct sockaddr_dl *)(ifm + 1);
ptr = (unsigned char *)LLADDR(sdl);
NSString *outstring = [NSString stringWithFormat:@"%02x:%02x:%02x:%02x:%02x:%02x", *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)];
// release pointer
free(buf);
return [outstring lowercaseString];
}
@end
核心代码iOS和OSX通用,改一下UI就可以 了,运行结果:
iOS:
OSX:
下面说说另外一种方法,用命令行获取设备IP地址和MAC地址,思路是ifconfig命令,获取en0的信息,然后筛选出IP和MAC:
#pragma mark - Another way to get the device IP address
- (NSString *)getIPAddress {
char ipAddress[16];
if ([self getIPAddressCommandResult:ipAddress] == -1) {
NSLog(@"获取IP地址失败");
}
return [NSString stringWithUTF8String:ipAddress];
}
- (int)getIPAddressCommandResult:(char *)result {
char buffer[16];
FILE* pipe = popen("ifconfig en0 | grep inet' ' | cut -d' ' -f 2", "r");
if (!pipe)
return -1;
while(!feof(pipe)) {
if(fgets(buffer, 1024, pipe)){
//strcat(result, buffer);
strncpy(result, buffer, sizeof(buffer) - 1);
result[sizeof(buffer) - 1] = '\0';
}
}
pclose(pipe);
return 0;
}
#pragma mark - Another way to get the device MAC address
- (NSString *)getMacAddress {
char macAddress[18];
if ([self getMACAddressCommandResult:macAddress] == -1) {
NSLog(@"获取MAC地址失败");
}
return [NSString stringWithUTF8String:macAddress];
}
- (int)getMACAddressCommandResult:(char *)result {
char buffer[18];
FILE* pipe = popen("ifconfig en0 | grep ether | cut -d' ' -f 2", "r");
if (!pipe)
return -1;
while(!feof(pipe)) {
if(fgets(buffer, 1024, pipe)){
//strcat(result, buffer);
strncpy(result, buffer, sizeof(buffer) - 1);
result[sizeof(buffer) - 1] = '\0';
}
}
pclose(pipe);
return 0;
}