下面兩種方法確實能拿到 MAC 位址,但僅限 iOS 7 以下。從 iOS 7 開始,Apple 對 sysctl 與 ioctl 做了處理,兩者回傳的都是固定值 02:00:00:00:00:00。
官方文件的原文是:
Two low-level networking APIs that used to return a MAC address now return the fixed value 02:00:00:00:00:00. The APIs in question are sysctl(NET_RT_IFLIST) and ioctl(SIOCGIFCONF). Developers using the value of the MAC address should migrate to identifiers such as -[UIDevice identifierForVendor]. This change affects all apps running on iOS 7.
所以 iOS 7 之後想靠 MAC 位址做裝置唯一標識是行不通的,官方建議改用 identifierForVendor。實務上更常見的組合是:需要廣告標識就用 advertisingIdentifier(使用者可重置、可關閉),需要裝置維度就用 identifierForVendor(同一開發者的 App 之間一致,卸載全部 App 後會重置),需要跨安裝穩定識別則自行產生 UUID 存到 Keychain。
以下兩種方法都需要先引入三個標頭:
#include <sys/sysctl.h> #include <net/if.h> #include <net/if_dl.h>
// 取得本機 MAC 位址
// 靈感來自 FreeBSD hackers 郵件列表,先前更新時被誤改,感謝 mlamb 協助修正
- (NSString *)macaddress {
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\n");
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)];
// 若要無分隔符格式,改用:
// [NSString stringWithFormat:@"%02x%02x%02x%02x%02x%02x", ...]
NSLog(@"outString:%@", outstring);
free(buf);
return [outstring uppercaseString];
}
寫法相近,但把每一步的錯誤都收進 errorFlag,除錯時比較容易定位:
- (NSString *)getMacAddress {
int mgmtInfoBase[6];
char *msgBuffer = NULL;
size_t length;
unsigned char macAddress[6];
struct if_msghdr *interfaceMsgStruct;
struct sockaddr_dl *socketStruct;
NSString *errorFlag = NULL;
// 設定管理資訊庫(mib)
mgmtInfoBase[0] = CTL_NET; // 網路子系統
mgmtInfoBase[1] = AF_ROUTE; // 路由表資訊
mgmtInfoBase[2] = 0;
mgmtInfoBase[3] = AF_LINK; // 資料鏈路層資訊
mgmtInfoBase[4] = NET_RT_IFLIST; // 所有已設定的介面
// 取得 en0 的介面索引
if ((mgmtInfoBase[5] = if_nametoindex("en0")) == 0) {
errorFlag = @"if_nametoindex failure";
} else {
// 先問一次要多大空間
if (sysctl(mgmtInfoBase, 6, NULL, &length, NULL, 0) < 0) {
errorFlag = @"sysctl mgmtInfoBase failure";
} else {
if ((msgBuffer = malloc(length)) == NULL) {
errorFlag = @"buffer allocation failure";
} else {
// 真正把資訊填進 buffer
if (sysctl(mgmtInfoBase, 6, msgBuffer, &length, NULL, 0) < 0) {
errorFlag = @"sysctl msgBuffer failure";
}
}
}
}
if (errorFlag != NULL) {
NSLog(@"Error: %@", errorFlag);
return errorFlag;
}
// 把 buffer 對應到介面訊息結構
interfaceMsgStruct = (struct if_msghdr *)msgBuffer;
// 再對應到鏈路層 socket 結構
socketStruct = (struct sockaddr_dl *)(interfaceMsgStruct + 1);
// 複製鏈路層位址到陣列
memcpy(&macAddress, socketStruct->sdl_data + socketStruct->sdl_nlen, 6);
NSString *macAddressString = [NSString stringWithFormat:@"%02x:%02x:%02x:%02x:%02x:%02x",
macAddress[0], macAddress[1], macAddress[2],
macAddress[3], macAddress[4], macAddress[5]];
NSLog(@"Mac Address: %@", macAddressString);
free(msgBuffer);
return macAddressString;
}
兩種方法的本質一樣:透過 sysctl 查 NET_RT_IFLIST 取得介面列表,再從 sockaddr_dl 結構裡取出 en0 的鏈路層位址。差別只在錯誤處理的細緻度。要注意 malloc 出來的内存務必 free,方法一在 sysctl 第二次失敗時會漏掉 buf,這類早退路徑在 ARC 下不會幫你管理 C 層記憶體。