iOS获取设备唯一标识

获取iOS唯一设备标识的方式有很多。以下列举我知道的几种方式

1、UDID

这个获取方式已被苹果禁止,不作考虑

2、IDFA

广告标识符,主要用于广告相关功能,并且这个值是设备内所有app共用的。

用户可以通过系统设置进行还原重置,并非唯一不变。

用户也可以通过隐私设置关闭该值的获取,这样app就无法获取到广告标识符。

如有需要,可以通过以下方式获取:

库支持: 

[[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString]

3、UUID(唯一但可变)

这种方式是最简单的一种方式,但是这个值并不是固定不变的,每一次应用卸载重新安装,它会变化,就像devicetoken一样。

获取方式:

[UIDevice currentDevice].identifierForVendor.UUIDString

4、MAC地址+BundleId(唯一)

MAC地址在网络上用来区分设备的唯一性,接入网络的设备都有一个MAC地址,他们肯定都是不同的,是唯一的。

而每个iOS APP都具有唯一的BundleId,所有通过MAC地址+BundleID组合的方式,可以得到APP在该设备上唯一的ID。

5、SAMKeychain库(唯一)

这种方式是通过向设备钥匙串注册,生成一个唯一的ID,即使卸载App然后重装,也不会改变这个值,基本满足了唯一不变的需求。

获取方式:

库支持:SAMKeychain

+ (NSString*)deviceChainId{

    NSString*bundleIden = @"com.app.bundleid";

    NSString* currentDeviceUUIDStr = [SAMKeychainpasswordForService:bundleIdenaccount:@"uuid"];    if(currentDeviceUUIDStr ==nil|| [currentDeviceUUIDStrisEqualToString:@""]) {

     NSUUID*currentDeviceUUID  = [UIDevicecurrentDevice].identifierForVendor;

  currentDeviceUUIDStr = currentDeviceUUID.UUIDString;

        currentDeviceUUIDStr = [currentDeviceUUIDStrstringByReplacingOccurrencesOfString:@"-"withString:@""];

        currentDeviceUUIDStr = [currentDeviceUUIDStrlowercaseString];    [SAMKeychainsetPassword:currentDeviceUUIDStrforService:bundleIdenaccount:@"uuid"];

    }

    returncurrentDeviceUUIDStr;

}

你可能感兴趣的:(iOS获取设备唯一标识)