簡體   English   中英

為什么這段代碼可以在 Xcode 模擬器上運行,但不能在設備上運行?

[英]Why does this code works on Xcode simulator, but does not work on device?

我真的希望有人向我解釋。 我正在編寫一個使用它的設備 mac 地址的應用程序,並且此代碼在模擬器上完美運行,但在設備上不起作用。

我從問題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

所以我的問題是為什么會這樣,在模擬器和設備上運行有什么區別? 先感謝您。

header 文件在 iOS 設備 SDK 中完全丟失。Apple 不希望您使用它,無論出於何種原因。 如果你只是想讓代碼工作,你可以嘗試提取所需的定義:

#define IFT_ETHER   0x6     /* Ethernet CSMACD */

並刪除行

#include <net/if_types.h>

完全地。 盡管這破壞了與平台的兼容性,在平台上這個常量可能被定義為其他一些值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM