如何在 iOS 设备中获取唯一 ID?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19402327/
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
How to get unique id in iOS device?
提问by Gowtham R
I have used mac address to identify iOS devices in server side. When run my application with iOS 7 unable to retrieve the correct mac address. Alternately i used
我已经使用 mac 地址来识别服务器端的 iOS 设备。使用 iOS 7 运行我的应用程序时,无法检索正确的 mac 地址。或者我用过
NSUUID *oNSUUID = [[UIDevice currentDevice] identifierForVendor];
[strApplicationUUID setString:[oNSUUID UUIDString]];
property. But this value also changed after every new installation of application. Now i want detect particular device in server side? How can i get some unique id per device in iOS?
财产。但每次新安装应用程序后,此值也会更改。现在我想检测服务器端的特定设备?如何在 iOS 中为每台设备获取一些唯一 ID?
回答by Guy Kogus
You can no longer get a unique ID per device. identifierForVendor
is the best you're going to get. Apple has systematically disabled identifying a specific device so that users' IDs can't be passed around by marketers.
您无法再获得每台设备的唯一 ID。identifierForVendor
是你要得到的最好的。Apple 已系统地禁用识别特定设备,以便营销人员无法传递用户的 ID。
To get the identifier ID as a string, you can use
要将标识符 ID 作为字符串获取,您可以使用
let deviceId = UIDevice.current.identifierForVendor?.uuidString
UPDATE
更新
If you want a universal ID then you have the option to use advertisingIdentifier
. However, if the user has limited ad tracking in their device settings then this value will simply return all zeroes.
如果您需要通用 ID,则可以选择使用advertisingIdentifier
. 但是,如果用户在其设备设置中限制了广告跟踪,则该值将简单地返回全零。
import AdSupport
let identifierManager = ASIdentifierManager.shared()
if identifierManager.isAdvertisingTrackingEnabled {
let deviceId = identifierManager.advertisingIdentifier.uuidString
}
N.B.Apple recommends that this code only be used by companies building advertisement frameworks, rather than the app developers themselves. If you use this within your code for non-ad-related purposes then prepare to have your app rejected.
NBApple 建议此代码仅供构建广告框架的公司使用,而不是应用程序开发人员自己使用。如果您在代码中将其用于与广告无关的目的,请准备好让您的应用被拒绝。
回答by RajeshRam
The best way to get the device id is identifierForVendor
获取设备 ID 的最佳方式是identifierForVendor
UIDevice *device = [UIDevice currentDevice];
NSString *currentDeviceId = [[device identifierForVendor]UUIDString];
UPDATE
更新
For Swift 4.1 use
对于 Swift 4.1 使用
UIDevice.current.identifierForVendor?.uuidString
回答by Hardik Bar
Getting device ID in
获取设备 ID
Swift 3
斯威夫特 3
let device_id = UIDevice.currentDevice().identifierForVendor?.UUIDString
Swift 4:
斯威夫特 4:
let device_id = UIDevice.current.identifierForVendor!.uuidString
回答by Levent Ozsoy
Getting Device Id in Swift 3.x
在 Swift 3.x 中获取设备 ID
let deviceId = UIDevice.current.identifierForVendor?.uuidString
回答by Kamil Harasimowicz
You can use this lib: https://github.com/fabiocaccamo/FCUUID
你可以使用这个库:https: //github.com/fabiocaccamo/FCUUID
Please look on the table in the Persistence section. uuidForDevice
will cover the most of needed cases and it is the best replacement to udid from old versions of iOS.
请查看持久性部分中的表格。uuidForDevice
将涵盖大部分需要的情况,它是旧版 iOS 中 udid 的最佳替代品。
Examples in Readme
are in Objective-C but it works with Swift too. I have used it since Swift first release until now (Swift 4.1).
示例在Readme
Objective-C 中,但它也适用于 Swift。从 Swift 首次发布到现在(Swift 4.1),我一直在使用它。
回答by Zgpeace
For Swift 5
对于 Swift 5
let deviceId = UIDevice.current.identifierForVendor?.uuidString