代码之家  ›  专栏  ›  技术社区  ›  Daniel

正在iOS中获取设备ID或Mac地址[重复]

  •  28
  • Daniel  · 技术社区  · 15 年前

    我有一个使用rest与服务器通信的应用程序,我想获得iPhone的mac地址或设备ID以进行唯一性验证,如何做到这一点?

    6 回复  |  直到 7 年前
        1
  •  41
  •   Sinkingpoint    11 年前

    [[UIDevice currentDevice] uniqueIdentifier] 保证每个设备都是唯一的。

        2
  •  40
  •   Community CDub    8 年前

    uniqueIdentifier(在iOS 5.0中不推荐使用。相反,请创建特定于应用程序的唯一标识符。)

    文件建议使用 CFUUIDCreate [[UIDevice currentDevice] uniqueIdentifier]

    下面是您如何在应用程序中生成唯一id的方法

    CFUUIDRef uuidRef = CFUUIDCreate(kCFAllocatorDefault);
    NSString *uuidString = (NSString *)CFUUIDCreateString(NULL,uuidRef);
    
    CFRelease(uuidRef);
    

    请注意,必须将UUIString保存在用户默认值或其他位置,因为无法再次生成相同的UUIString。

    UIPasteboard

    在iOS 6中,他们引入了 NSUUID Class 设计用于创建UUID字符串的

    @property(nonatomic, readonly, retain) NSUUID *identifierForVendor UIDevice

    同一供应商在同一设备上运行。返回一个不同的值 对于来自不同供应商的同一设备上的应用,以及 不同设备上的应用程序,不考虑供应商。

    后台,在用户首次解锁设备之前 设备重新启动后。如果值为nil,请等待并获取 该值将在稍后再次显示。

    在iOS 6中也可以使用 ASIdentifierManager 从AdSupport.framework初始化。给你

    @property(nonatomic, readonly) NSUUID *advertisingIdentifier
    

    讨论与UIDevice的identifierForVendor属性不同, 更改例如,如果用户擦除设备,则不应

    如果应用程序正在中运行,则此属性的值可能为零 后台,在用户首次解锁设备之前 设备重新启动后。如果值为nil,请等待并获取

    advertisingIdentifier 可能会回来

    00000000-0000-0000-0000-000000000000

    The advertisingIdentifier and identifierForVendor return "00000000-0000-0000-0000-000000000000"

        3
  •  18
  •   Blazer    13 年前

    你可以使用Mac地址

    #import <Foundation/Foundation.h>
    
    @interface MacAddressHelper : NSObject
    
    + (NSString *)getMacAddress;
    
    @end
    

    #import "MacAddressHelper.h"
    #import <sys/socket.h>
    #import <sys/sysctl.h>
    #import <net/if.h>
    #import <net/if_dl.h>
    
    @implementation MacAddressHelper
    
    + (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;
    
      // Setup the management Information Base (mib)
      mgmtInfoBase[0] = CTL_NET;        // Request network subsystem
      mgmtInfoBase[1] = AF_ROUTE;       // Routing table info
      mgmtInfoBase[2] = 0;              
      mgmtInfoBase[3] = AF_LINK;        // Request link layer information
      mgmtInfoBase[4] = NET_RT_IFLIST;  // Request all configured interfaces
    
      // With all configured interfaces requested, get handle index
      if ((mgmtInfoBase[5] = if_nametoindex("en0")) == 0) 
        errorFlag = @"if_nametoindex failure";
      else
      {
        // Get the size of the data available (store in len)
        if (sysctl(mgmtInfoBase, 6, NULL, &length, NULL, 0) < 0) 
          errorFlag = @"sysctl mgmtInfoBase failure";
        else
        {
          // Alloc memory based on above call
          if ((msgBuffer = malloc(length)) == NULL)
            errorFlag = @"buffer allocation failure";
          else
          {
            // Get system information, store in buffer
            if (sysctl(mgmtInfoBase, 6, msgBuffer, &length, NULL, 0) < 0)
              errorFlag = @"sysctl msgBuffer failure";
          }
        }
      }
      // Befor going any further...
      if (errorFlag != NULL)
      {
        NSLog(@"Error: %@", errorFlag);
        return errorFlag;
      }
      // Map msgbuffer to interface message structure
      interfaceMsgStruct = (struct if_msghdr *) msgBuffer;
      // Map to link-level socket structure
      socketStruct = (struct sockaddr_dl *) (interfaceMsgStruct + 1);  
      // Copy link layer address data in socket structure to an array
      memcpy(&macAddress, socketStruct->sdl_data + socketStruct->sdl_nlen, 6);  
      // Read from char array into a string object, into traditional Mac address format
      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);  
      // Release the buffer memory
      free(msgBuffer);
      return macAddressString;
    }
    
    @end
    

    使用:

    NSLog(@"MAC address: %@",[MacAddressHelper getMacAddress]);
    
        4
  •  5
  •   Alexander Volkov    12 年前

    使用以下命令:

    NSUUID *id = [[UIDevice currentDevice] identifierForVendor];
    NSLog(@"ID: %@", id);
    
        5
  •  4
  •   Community CDub    8 年前

    在IOS 5中 [[UIDevice currentDevice] uniqueIdentifier]

    -identifierForVendor -identifierForAdvertising

    在这里可以找到许多有用的信息:

    iOS6 UDID - What advantages does identifierForVendor have over identifierForAdvertising?

        6
  •  -10
  •   kleopatra Aji kattacherry    12 年前

    在这里,我们可以使用Asp.net C#代码找到IOS设备的mac地址。。。

    .aspx.cs

    -
     var UserDeviceInfo = HttpContext.Current.Request.UserAgent.ToLower(); // User's Iphone/Ipad Info.
    
    var UserMacAdd = HttpContext.Current.Request.UserHostAddress;         // User's Iphone/Ipad Mac Address
    
    
    
      GetMacAddressfromIP macadd = new GetMacAddressfromIP();
            if (UserDeviceInfo.Contains("iphone;"))
            {
                // iPhone                
                Label1.Text = UserDeviceInfo;
                Label2.Text = UserMacAdd;
                string Getmac = macadd.GetMacAddress(UserMacAdd);
                Label3.Text = Getmac;
            }
            else if (UserDeviceInfo.Contains("ipad;"))
            {
                // iPad
                Label1.Text = UserDeviceInfo;
                Label2.Text = UserMacAdd;
                string Getmac = macadd.GetMacAddress(UserMacAdd);
                Label3.Text = Getmac;
            }
            else
            {
                Label1.Text = UserDeviceInfo;
                Label2.Text = UserMacAdd;
                string Getmac = macadd.GetMacAddress(UserMacAdd);
                Label3.Text = Getmac;
            }
    

    .class文件

    public string GetMacAddress(string ipAddress)
        {
            string macAddress = string.Empty;
            if (!IsHostAccessible(ipAddress)) return null;
    
            try
            {
                ProcessStartInfo processStartInfo = new ProcessStartInfo();
    
                Process process = new Process();
    
                processStartInfo.FileName = "arp";
    
                processStartInfo.RedirectStandardInput = false;
    
                processStartInfo.RedirectStandardOutput = true;
    
                processStartInfo.Arguments = "-a " + ipAddress;
    
                processStartInfo.UseShellExecute = false;
    
                process = Process.Start(processStartInfo);
    
                int Counter = -1;
    
                while (Counter <= -1)
                {                  
                        Counter = macAddress.Trim().ToLower().IndexOf("mac address", 0);
                        if (Counter > -1)
                        {
                            break;
                        }
    
                        macAddress = process.StandardOutput.ReadLine();
                        if (macAddress != "")
                        {
                            string[] mac = macAddress.Split(' ');
                            if (Array.IndexOf(mac, ipAddress) > -1)                                
                            {
                                if (mac[11] != "")
                                {
                                    macAddress = mac[11].ToString();
                                    break;
                                }
                            }
                        }
                }
                process.WaitForExit();
                macAddress = macAddress.Trim();
            }
    
            catch (Exception e)
            {
    
                Console.WriteLine("Failed because:" + e.ToString());
    
            }
            return macAddress;
    
        }