【发布时间】:2014-01-23 13:26:01
【问题描述】:
如何在 iOS7 中以编程方式获取设备 UDID。[[UIDevice currentDevice] uniqueIdentifier] 我曾使用此代码 这是已弃用的 iOS7。如何获取设备 UDID。当我删除应用程序并重新安装时,UDID 字符串发生了变化,这意味着 UDID 变得不同了。
【问题讨论】:
-
给予声望,只有对他人有用,才能获得更多声望
如何在 iOS7 中以编程方式获取设备 UDID。[[UIDevice currentDevice] uniqueIdentifier] 我曾使用此代码 这是已弃用的 iOS7。如何获取设备 UDID。当我删除应用程序并重新安装时,UDID 字符串发生了变化,这意味着 UDID 变得不同了。
【问题讨论】:
在 iOS 7 中,Apple 现在在查询 MAC 时始终返回一个固定值,以专门阻止将 MAC 作为 ID 方案的基础。所以你现在真的应该使用-[UIDevice identifierForVendor] 或创建一个每次安装的 UUID。
检查this SO 问题。
【讨论】:
由于安全/隐私原因,UDID 在 iOS 6+ 中不再可用。相反,请使用 identifierForVendor 或 adsIdentifier。
请通过this链接。
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;
}
}
【讨论】:
所有版本都 100% 有效
苹果推荐的其他最佳选择使用ASIdentifierManager Class Reference
ios7-app-backward-compatible-with-ios5-regarding-unique-identifier
这个链接告诉你如何处理和使用自定义框架
uidevice-uniqueidentifier-property-is-deprecated-what-now
iOS 9
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
}
【讨论】:
使用 identifierForVendor 或 adsIdentifier。
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.
另外,有关 identifierForVendor 和 adsIdentifier 的信息,请参阅 Apple 文档。
【讨论】:
有两种解决方案:
【讨论】:
在 Swift 中,您可以像这样获取设备 UUID
let uuid = UIDevice.currentDevice().identifierForVendor.UUIDString
println(uuid)
【讨论】:
获取 UDID:(如果你使用这个可能是 App Store 的人不会允许的 --> 根据我的担心)
- (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];
【讨论】:
Swift 2.2+ 获取 UUID 的正确方法:
if let UUID = UIDevice.currentDevice().identifierForVendor {
print("UUID: \(UUID.UUIDString)")
}
【讨论】:
在 Swift 3.0 中获取 UUID:
let UUIDValue = UIDevice.current.identifierForVendor!.uuidString
【讨论】:
在 Swift 3.0 中:
UIDevice.current.identifierForVendor!.uuidString
旧版本
UIDevice.currentDevice().identifierForVendor
你想要一个字符串:
UIDevice.currentDevice().identifierForVendor!.UUIDString
【讨论】: