在 iOS 7 中始终获得唯一的设备 ID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19606773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:36:32  来源:igfitidea点击:

Always get a unique device id in iOS 7

iosiphoneobjective-cuniqueidentifier

提问by jaydev

Our iOS application is for specific users. So, we used device unique identifierfor user identification. This approach works fine till iOS 6, because we are getting the same valueevery time.

我们的 iOS 应用程序适用于特定用户。因此,我们使用设备唯一标识符来识别用户。这种方法在 iOS 6 之前都可以正常工作,因为我们每次都得到相同的值

NSString *strUniqueIdentifier = [[UIDevice currentDevice] uniqueIdentifier];

In iOS 7, the above method is returning different valuesand we are getting issues in user identification. iOS 7 provides the following alternate.

在 iOS 7 中,上述方法返回不同的值,我们在用户识别方面遇到问题。iOS 7 提供了以下替代.

NSUUID *oNSUUID = [[UIDevice currentDevice] identifierForVendor];
[strApplicationUUID setString:[oNSUUID UUIDString]];

We replaced uniqueIdentifierwith identifierForVendor, and created an Ad-hoc build. We then installed the build on both iOS 7 and iOS 6 devices. So far in iOS 7, we are getting the same valueevery time, but iOS 6 gives different valuesevery time we delete and reinstall the app.

我们替换uniqueIdentifieridentifierForVendor,并创建了一个临时构建。然后我们在 iOS 7 和 iOS 6 设备上安装了该版本。到目前为止,在 iOS 7 中,我们每次都得到相同的值,但 iOS 6每次删除和重新安装应用程序时都会给出不同的值

回答by nerowolfe

Use this little helper method to keep identifier in Keychain between install/delete sessions of app

使用这个小助手方法在应用程序的安装/删除会话之间保留钥匙串中的标识符

-(NSString *)getUniqueDeviceIdentifierAsString
{
    NSString *appName=[[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString*)kCFBundleNameKey];

    NSString *strApplicationUUID = [SSKeychain passwordForService:appName account:@"incoding"];
    if (strApplicationUUID == nil)
    {
        strApplicationUUID  = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
        [SSKeychain setPassword:strApplicationUUID forService:appName account:@"incoding"];
    }

    return strApplicationUUID;
}

Add the SSKeychainlibrary to your project, e.g. via Cocoapods with pod 'SSKeychain'

SSKeychain库添加到您的项目中,例如通过 Cocoapodspod 'SSKeychain'

回答by k.t

What is UDID replaced with?

UDID 用什么代替?

For now it looks like Apple plan to replace UDID's with two different identifiers: Vendor IDs and Advertising IDs.

目前看来,Apple 计划用两种不同的标识符替换 UDID:供应商 ID 和广告 ID。

Vendor IDs

供应商 ID

ID that is identical between apps from the same developer. Erased with removal of the last app for that Team ID.

来自同一开发者的应用程序之间的 ID 相同。删除该团队 ID 的最后一个应用程序。

Advertising IDs

广告 ID

Unique to the device. Available to all applications; used for advertising — iAd has converted from UDID for iOS 6 and later. Reset with “Erase All Content & Settings”.

设备独有。适用于所有应用程序;用于广告 — iAd 已从 iOS 6 及更高版本的 UDID 转换而来。使用“删除所有内容和设置”重置。


For the most part, Vendor IDs will allow developers with multiple apps to identify you as the same user across all their apps. This can help developers understand their audience better and provide a better service.


在大多数情况下,供应商 ID 将允许拥有多个应用程序的开发人员在他们的所有应用程序中将您标识为同一用户。这可以帮助开发人员更好地了解他们的受众并提供更好的服务。

Advertising IDs will still create a unique identifier for the device but unlike the UDID, this can be turned off or reset, just as cookies can be controlled in the browser on your desktop or laptop.

广告 ID 仍会为设备创建唯一标识符,但与 UDID 不同的是,它可以关闭或重置,就像可以在台式机或笔记本电脑上的浏览器中控制 cookie 一样。

With the announcement of iOS7 earlier this week, Apple took an additional step, which was to turn off another unique identifier — the “MAC Address” that app developers and advertising companies could use instead of the new Advertising ID. By closing this loophole, Apple is taking the good step of forcing these companies to only use a cross-app identifier that users can control.

随着本周早些时候 iOS7 的发布,Apple 采取了额外的措施,即关闭另一个唯一标识符——应用程序开发人员和广告公司可以使用的“MAC 地址”,而不是新的广告 ID。通过堵住这个漏洞,Apple 采取了很好的措施,迫使这些公司只使用用户可以控制的跨应用程序标识符。

What remains to be seen is whether the targeting companies will continue to seek ways around Apple's mandate. Other technologies, like “device fingerprinting,” have been developed to uniquely identify your device outside of Apple's framework. Whether Apple will crack down on these methods remains to be seen.

还有待观察的是,目标公司是否会继续寻求绕过苹果授权的方法。其他技术,如“设备指纹识别”,已被开发用于在 Apple 框架之外唯一地识别您的设备。苹果是否会打击这些方法还有待观察。

回答by neilco

From the UIDevice Class referencefor identifierForVendor:

的UIDevice类参考identifierForVendor

The value changes when the user deletes all of that vendor's apps from the device and subsequently reinstalls one or more of them. The value can also when installing test builds using Xcode or when installing an app on a device using ad-hoc distribution. Therefore, if your app stores the value of this property anywhere, you should gracefully handle situations where the identifier changes.

当用户从设备中删除该供应商的所有应用程序并随后重新安装其中一个或多个应用程序时,该值会发生变化。当使用 Xcode 安装测试版本或使用临时分发在设备上安装应用程序时,该值也可以。因此,如果您的应用程序将这个属性的值存储在任何地方,您应该优雅地处理标识符更改的情况。

回答by tilo

Are there additional apps from the same vendor installed on iOS7 devices? According to the docs:

iOS7 设备上是否安装了来自同一供应商的其他应用程序?根据文档

The value in this property remains the same while the app (or another app from the same vendor) is installed on the iOS device. The value changes when the user deletes all of that vendor's apps from the device and subsequently reinstalls one or more of them. The value can also when installing test builds using Xcode or when installing an app on a device using ad-hoc distribution. Therefore, if your app stores the value of this property anywhere, you should gracefully handle situations where the identifier changes.

当应用程序(或来自同一供应商的另一个应用程序)安装在 iOS 设备上时,此属性中的值保持不变。当用户从设备中删除该供应商的所有应用程序并随后重新安装其中一个或多个应用程序时,该值会发生变化。当使用 Xcode 安装测试版本或使用临时分发在设备上安装应用程序时,该值也可以。因此,如果您的应用程序将这个属性的值存储在任何地方,您应该优雅地处理标识符更改的情况。

Do you provide the app via the AppStore? If not:

您是否通过 AppStore 提供应用程序?如果不:

If the app was not installed from the app store (such as when the app is still in development), the vendor is determined based on the app's bundle ID. The bundle ID is assumed to be in reverse-DNS format, and the first two components are used to generate a vendor ID. For example, com.example.app1 and com.example.app2 would appear to have the same vendor ID.

如果应用程序不是从应用程序商店安装的(例如应用程序仍在开发中),则根据应用程序的捆绑 ID 确定供应商。捆绑 ID 假定为反向 DNS 格式,前两个组件用于生成供应商 ID。例如,com.example.app1 和 com.example.app2 似乎具有相同的供应商 ID。

回答by Maggie

If there are no other applications signed by you installed on the device, it is ok for identifier for vendor to change. Also, identifier for vendor may change if you install you application through different distribution methods, ie. application may not have the same identifier for vendor when installed through XCode and when distributed via TestFlight or HockeyApp.

如果设备上没有安装您签名的其他应用程序,则可以更改供应商的标识符。此外,如果您通过不同的分发方法安装应用程序,供应商的标识符可能会更改,即。当通过 XCode 安装和通过 TestFlight 或 HockeyApp 分发时,应用程序可能不具有相同的供应商标识符。

回答by Hussain Shabbir

Try to clear caches of app on your machine and then check. It may be your cache problem

尝试清除您机器上的应用程序缓存,然后检查。可能是你的缓存问题

回答by FurloSK

To add something to nerowolfe's answer, there is a great Keychain Wrapper named MCSMKeychainItem, that, on top of several other things, allows you to generate and retrieve Unique Device ID with single line of code:

要在nerowolfe 的回答中添加一些内容,有一个名为 的出色钥匙串包装器MCSMKeychainItem,除了其他几项之外,它允许您使用一行代码生成和检索唯一设备 ID:

[MCSMApplicationUUIDKeychainItem applicationUUID];

so the basic usage will be something like

所以基本用法类似于

+ (NSString *)deviceId {
  // load unique device ID or generate new one
  return [MCSMApplicationUUIDKeychainItem applicationUUID];
}

It works on the basis of Keychain, where it stores once-generated unique identifier (as nerowolf suggested). It's open-source and you can download it here on github.

它基于 Keychain 工作,它存储一次生成的唯一标识符(如 nerowolf 建议的那样)。它是开源的,你可以在 github 上下载。

Note:I am not the author of the extension, nor do I in any way know him/her.

注意:我不是扩展程序的作者,也不以任何方式认识他/她。