我真的很希望,有人向我解釋。 我正在編寫一個使用它的設備mac地址的應用程序,並且此代碼完全可以在模擬器上工作,但不適用於設備。爲什麼此代碼可以在Xcode模擬器上工作,但無法在設備上工作?
我從問題Get router mac (without system call for ARP) in Objective-C
#include <stdio.h>
#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <net/if_dl.h>
#include <ifaddrs.h>
#include <net/if_types.h>
char* getMacAddress(char* macAddress, char* ifName)
{
int success;
struct ifaddrs *addrs;
struct ifaddrs *cursor;
const struct sockaddr_dl *dlAddr;
const unsigned char* base;
int i;
success = getifaddrs(&addrs) == 0;
if (success)
{
cursor = addrs;
while (cursor != 0)
{
const struct sockaddr_dl *socAddr =
(const struct sockaddr_dl *)cursor->ifa_addr;
_Bool afLinkFamily = (cursor->ifa_addr->sa_family == AF_LINK);
/* Ethernet CSMA/CD */
_Bool sdlIFTEther = (socAddr->sdl_type == IFT_ETHER);
if ((afLinkFamily) &&
sdlIFTEther &&
strcmp(ifName, cursor->ifa_name) == 0)
{
dlAddr = (const struct sockaddr_dl *) cursor->ifa_addr;
base =
(const unsigned char*)&dlAddr->sdl_data[dlAddr->sdl_nlen];
strcpy(macAddress, "");
for (i = 0; i < dlAddr->sdl_alen; i++)
{
if (i != 0)
{
strcat(macAddress, ":");
}
char partialAddr[3];
sprintf(partialAddr, "%02X", base[i]);
strcat(macAddress, partialAddr);
}
}
cursor = cursor->ifa_next;
}
freeifaddrs(addrs);
}
return macAddress;
}
錯誤的代碼它給我:
'net/if_types.h' file not found
所以我的問題是,爲什麼會出現這種情況,就是在模擬器運行之間的差異,設備? 預先感謝您。
嗨你有什麼解決方案,面臨同樣的問題。 – Nikunj 2013-12-18 10:23:48