先說明一個重要前提:下面兩種方法確實都能取得手機的 MAC 位址,但有版本限制——在 iOS 7 之前才有效。
從 iOS 7 開始,Apple 對 sysctl 與 ioctl 這兩個低階網路 API 做了技術處理,回傳的 MAC 位址一律是固定值 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 位址作為裝置的唯一識別碼,必須改用其他替代方案。
#include <sys/sysctl.h> #include <net/if.h> #include <net/if_dl.h>
// 取得本機 MAC 位址
// 靈感來自 FreeBSD hackers 郵件列表
- (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");
free(buf);
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)];
NSLog(@"outString: %@", outstring);
free(buf);
return [outstring uppercaseString];
}
- (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;
// 設定 Management Information Base (mib)
mgmtInfoBase[0] = CTL_NET; // 要求網路子系統
mgmtInfoBase[1] = AF_ROUTE; // 路由表資訊
mgmtInfoBase[2] = 0;
mgmtInfoBase[3] = AF_LINK; // 要求link layer資訊
mgmtInfoBase[4] = NET_RT_IFLIST; // 要求所有已設定的介面
// 取得介面的 handle index
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 {
// 取得系統資訊並存入緩衝區
if (sysctl(mgmtInfoBase, 6, msgBuffer, &length, NULL, 0) < 0) {
errorFlag = @"sysctl msgBuffer failure";
}
}
}
}
if (errorFlag != NULL) {
NSLog(@"Error: %@", errorFlag);
free(msgBuffer);
return errorFlag;
}
// 對應到介面訊息結構
interfaceMsgStruct = (struct if_msghdr *)msgBuffer;
// 對應到 link-level socket 結構
socketStruct = (struct sockaddr_dl *)(interfaceMsgStruct + 1);
// 把 link layer 位址資料複製到陣列
memcpy(&macAddress, socketStruct->sdl_data + socketStruct->sdl_nlen, 6);
// 轉為傳統的 MAC 位址格式
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 走訪網路介面。若目的是取得裝置識別碼,可以考慮以下選項:
identifierForVendor:同一開發者的 App 在同一裝置上會得到相同值,但卸載該開發者的所有 App 後會重置。適合做為分析用途的基礎識別。
Keychain 保存 UUID:把首次產生的 UUID 寫入 Keychain,因為 Keychain 的內容在 App 被刪除後仍會保留,因此可以實現跨安裝的穩定識別。實作時需注意 iOS 7 之後的 kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly 等可見性設定。
ASIdentifierManager:廣告識別碼(IDFA),使用者可在系統設定中重設或關閉,且需遵循 ATT 框架的透明度規範才能存取。
需要特別注意的是,上述任何方案都不應用於規避 Apple 的隱私政策。若App 收集可識別資訊,必須在 App Store 的隱私權條目與 App Privacy Details 中如實揭露。