如何在iOS7中以编程方式获取设备UDID。[[UIDevice currentDevice] uniqueIdentifier]
我曾经使用过这段代码,但它已经被废弃了。如何获取设备UDID呢?当我删除并重新安装应用程序时,UDID字符串会更改,这意味着获得了不同的UDID。
对于所有版本,它都能够百分百地工作。
苹果推荐的另一个最佳选择是使用ASIdentifierManager Class Reference
此链接告诉您如何处理和使用自定义框架。
uidevice-uniqueidentifier-property-is-deprecated-what-now
iOS9
NSUUID *uuid = [[NSUUID alloc]initWithUUIDString:@"20B0DDE7-6087-4607-842A-E97C72E4D522"];
NSLog(@"%@",uuid);
NSLog(@"%@",[uuid UUIDString]);
或
它仅支持iOS 6.0及以上版本
使用的代码为[[[UIDevice currentDevice] identifierForVendor] UUIDString];
NSUUID *deviceId;
#if TARGET_IPHONE_SIMULATOR
deviceId = [NSUUID initWithUUIDString:@"UUID-STRING-VALUE"];
#else
deviceId = [UIDevice currentDevice].identifierForVendor;
#endif
iOS 5如何使用
if ([[UIDevice currentDevice] respondsToSelector:@selector(identifierForVendor)]) {
// This is will run if it is iOS6
return [[[UIDevice currentDevice] identifierForVendor] UUIDString];
} else {
// This is will run before iOS6 and you can use openUDID or other
// method to generate an identifier
}
由于安全/隐私原因,iOS 6+中不再提供UDID。请改用identifierForVendor或advertisingIdentifier。
请访问此链接了解更多信息。
NSString* uniqueIdentifier = [[[UIDevice currentDevice] identifierForVendor] UUIDString]; // IOS 6+
NSLog(@"UDID:: %@", uniqueIdentifier);
iOS 8+更新
+ (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;
}
}
在Swift中,您可以像这样获取设备UUID:
let uuid = UIDevice.currentDevice().identifierForVendor.UUIDString
println(uuid)
请使用 identifierForVendor 或 advertisingIdentifier。
identifierForVendor:
An alphanumeric string that uniquely identifies a device to the app’s vendor. (read-only)
The value of this property is the same for apps that come from the same vendor running on the same device. A different value is returned for apps on the same device that come from different vendors, and for apps on different devices regardless of vendor.
广告标识符:
An alphanumeric string unique to each device, used only for serving advertisements. (read-only)
Unlike the identifierForVendor property of UIDevice, the same value is returned to all vendors. This identifier may change—for example, if the user erases the device—so you should not cache it.
UIDevice.current.identifierForVendor!.uuidString
old version
UIDevice.currentDevice().identifierForVendor
UIDevice.currentDevice().identifierForVendor!.UUIDString
获取UUID的正确方式:Swift 2.2+
if let UUID = UIDevice.currentDevice().identifierForVendor {
print("UUID: \(UUID.UUIDString)")
}
UUID
let UUIDValue = UIDevice.current.identifierForVendor!.uuidString
获取UDID:(如果您正在使用此方法,可能会被应用商店拒绝-->据我所知)
- (NSString *)udid
{
void *gestalt = dlopen("/usr/lib/libMobileGestalt.dylib", RTLD_GLOBAL | RTLD_LAZY);
CFStringRef (*MGCopyAnswer)(CFStringRef) = (CFStringRef (*)(CFStringRef))(dlsym(gestalt, "MGCopyAnswer"));
return CFBridgingRelease(MGCopyAnswer(CFSTR("UniqueDeviceID")));
}
**Entitlements:**
<key>com.apple.private.MobileGestalt.AllowedProtectedKeys</key>
<array>
<string>UniqueDeviceID</string>
</array>
获取 UUID:
self.uuidTxtFldRef.text = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
有两种解决方案: