在我以前的应用程序中,我使用下面提到的代码为iOS设备生成唯一的deviceId。但问题是,每次重新安装应用程序时它都会生成一个新代码。我该如何正确地执行此操作?
-(NSString*)uniqueIDForDevice //new..
{
NSString* uniqueIdentifier = nil;
if( [UIDevice instancesRespondToSelector:@selector(identifierForVendor)] ) { // >=iOS 7
uniqueIdentifier = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
}
else
{ //<=iOS6, Use UDID of Device
CFUUIDRef uuid = CFUUIDCreate(NULL);
//uniqueIdentifier = ( NSString*)CFUUIDCreateString(NULL, uuid);- for non- ARC
uniqueIdentifier = ( NSString*)CFBridgingRelease(CFUUIDCreateString(NULL, uuid));// for ARC
CFRelease(uuid);
}
return uniqueIdentifier;
}
答案 0 :(得分:2)
首先获得vendorId(uniqueID)而不是将其存储到keychain
// Fetch vendorID from keychain first, if it exists then use it or fetch vendorID
//if user delete app and again install app than he get vendorID from keychain(if he hasn't cleared it)
NSString *vendorID = [UICKeyChainStore stringForKey:@"KEY TO SAVE TO Keychain" service:nil];
NSLog(@"VendorID from Keychain - %@",vendorID);
//if vandorid from keychain is not nil
if (vendorID)
{
[[NSUserDefaults standardUserDefaults] setObject:vendorID forKey:@"vendorId"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
//else it goes for new vendorid and then stored it to keychan
else
{
vendorID = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
[UICKeyChainStore setString:vendorID forKey:@"KEY TO SAVE TO Keychain" service:nil];
[[NSUserDefaults standardUserDefaults] setObject:vendorID forKey:@"vendorId"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(@"VendorID Local - %@",vendorID);
}
我使用this类来保存和检索来自keyChain的vendorID!
只需看看这个。它会帮助你
答案 1 :(得分:2)
您可以使用以下
+ (NSString *)deviceUUID
{
if([[NSUserDefaults standardUserDefaults] objectForKey:[[NSBundle mainBundle] bundleIdentifier]])
return [[NSUserDefaults standardUserDefaults] objectForKey:[[NSBundle mainBundle] bundleIdentifier]];
@autoreleasepool {
CFUUIDRef uuidReference = CFUUIDCreate(nil);
CFStringRef stringReference = CFUUIDCreateString(nil, uuidReference);
NSString *uuidString = (__bridge NSString *)(stringReference);
[[NSUserDefaults standardUserDefaults] setObject:uuidString forKey:[[NSBundle mainBundle] bundleIdentifier]];
[[NSUserDefaults standardUserDefaults] synchronize];
CFRelease(uuidReference);
CFRelease(stringReference);
return uuidString;
}
}
答案 2 :(得分:1)
使用This链接到UICKeyChainStore。我添加了以下方法调用
appDelegate应用程序[self getDeviceIdFromKeychain];
中的 didFinishLaunchingWithOptions
-(void)getDeviceIdFromKeychain
{
NSString *deviceID = [UICKeyChainStore stringForKey:DEVICE_ID_FROM_KEYCHAIN_REFERENCE_APP service:nil];
NSLog(@"deviceID from Keychain = (%@)",deviceID);
//if vandorid from keychain is not nil
if (deviceID)
{
[[NSUserDefaults standardUserDefaults] setObject:deviceID forKey:DEVICE_ID];
[[NSUserDefaults standardUserDefaults] synchronize];
}
//else it goes for new vendorid and then stored it to keychan
else if (deviceID == (id)[NSNull null] || deviceID.length == 0 )
{
NSString *newDeviceId;
newDeviceId=[[self class] deviceUUID];
[[NSUserDefaults standardUserDefaults] setObject:newDeviceId forKey:DEVICE_ID];
[[NSUserDefaults standardUserDefaults] synchronize];
[UICKeyChainStore setString:newDeviceId forKey:DEVICE_ID_FROM_KEYCHAIN_REFERENCE_APP service:nil];
NSLog(@"new deviceID = (%@)",newDeviceId);
}
}
+ (NSString *)deviceUUID
{
if([[NSUserDefaults standardUserDefaults] objectForKey:[[NSBundle mainBundle] bundleIdentifier]])
return [[NSUserDefaults standardUserDefaults] objectForKey:[[NSBundle mainBundle] bundleIdentifier]];
@autoreleasepool {
CFUUIDRef uuidReference = CFUUIDCreate(nil);
CFStringRef stringReference = CFUUI
DCreateString(nil, uuidReference);
NSString *uuidString = (__bridge NSString *)(stringReference);
[[NSUserDefaults standardUserDefaults] setObject:uuidString forKey:[[NSBundle mainBundle] bundleIdentifier]];
[[NSUserDefaults standardUserDefaults] synchronize];
CFRelease(uuidReference);
CFRelease(stringReference);
return uuidString;
}
}
我使用它们就像这样......(在我的signinViewcontroller中)
NSString *deviceIDfromNSUserDefaults = [[NSUserDefaults standardUserDefaults]valueForKey:DEVICE_ID ];
NSLog(@"deviceID from NSUserDefaults = (%@)",deviceIDfromNSUserDefaults);
答案 3 :(得分:0)
我正在使用UUID生成唯一编号。如果我们不在应用程序中使用iAd,我怀疑是否允许使用供应商ID。 我通过生成UUID然后存储到钥匙串中来实现这一点。 钥匙串将存储您的UUID,直到用户不会进行硬重置,这种情况很少见。
由于Apple没有提供API来获取唯一的设备ID,我们必须找到这样的方法。