在iOS中获取设备ID或Mac地址

Dan*_*iel 29 mac-address objective-c device ios

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

Ste*_*eld 41

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

  • 对于这个问题的新来者,在iOS 5中不推荐使用UDID API.要获得唯一标识符,您需要转而使用iPhone的MAC地址. (22认同)
  • 在iOS 6+中使用`identifierForVendor`,返回NSUUID对象 (2认同)

Ale*_*nte 40

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

文档建议使用CFUUIDCreate而不是[[UIDevice currentDevice] uniqueIdentifier]

以下是您在应用中生成唯一ID的方法

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

CFRelease(uuidRef);
Run Code Online (Sandbox Code Playgroud)

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

您可以使用UIPasteboard存储生成的uuid.如果应用程序将被删除并重新安装,您可以从UIPasteboard读取旧的uuid.擦除设备时,粘贴板将被擦除.

在iOS 6中,他们引入了旨在创建UUID字符串的NSUUID类

他们还在iOS 6中添加@property(nonatomic, readonly, retain) NSUUID *identifierForVendorUIDevice

对于来自同一设备上运行的同一供应商的应用,此属性的值相同.对于来自不同供应商的同一设备上的应用程序以及不同供应商的不同设备上的应用程序,将返回不同的值.

如果应用程序在后台运行,则在用户在设备重新启动后第一次解锁设备之前,此属性的值可能为nil.如果值为nil,请稍后再次获取该值.

同样在iOS 6中,您可以使用AdSupport.framework中的ASIdentifierManager类.你有

@property(nonatomic, readonly) NSUUID *advertisingIdentifier
Run Code Online (Sandbox Code Playgroud)

讨论与UIDevice的identifierForVendor属性不同,将向所有供应商返回相同的值.此标识符可能会更改 - 例如,如果用户删除了设备 - 因此您不应对其进行缓存.

如果应用程序在后台运行,则在用户在设备重新启动后第一次解锁设备之前,此属性的值可能为nil.如果值为nil,请稍后再次获取该值.

编辑:

注意advertisingIdentifier可能会返回

00000000-0000-0000-0000-000000000000

因为iOS中似乎有一个错误.相关问题:advertisingIdentifier和identifierForVendor返回"00000000-0000-0000-0000-000000000000"


Bla*_*zer 19

对于你可以使用的Mac地址

#import <Foundation/Foundation.h>

@interface MacAddressHelper : NSObject

+ (NSString *)getMacAddress;

@end
Run Code Online (Sandbox Code Playgroud)

implentation

#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
Run Code Online (Sandbox Code Playgroud)

使用:

NSLog(@"MAC address: %@",[MacAddressHelper getMacAddress]);
Run Code Online (Sandbox Code Playgroud)

  • 从iOS7开始,此功能不再起作用。如iOS 7的发行说明所述:用于返回MAC地址的两个低级网络API现在返回固定值02:00:00:00:00:00。有问题的API是sysctl(NET_RT_IFLIST)和ioctl(SIOCGIFCONF)。使用MAC地址值的开发人员应迁移到标识符,例如-[UIDevice identifierForVendor]。此更改会影响在iOS 7上运行的所有应用。Objective-C运行时说明 (2认同)

Ale*_*kov 5

用这个:

NSUUID *id = [[UIDevice currentDevice] identifierForVendor];
NSLog(@"ID: %@", id);
Run Code Online (Sandbox Code Playgroud)


归档时间:

查看次数:

42700 次

最近记录:

8 年,1 月 前