ios 如何获取iPhone的UDID?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35667943/
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 08:36:36  来源:igfitidea点击:

How to get UDID of iPhone?

iosiphone

提问by sagar varsani

I want unique device ID for iPhone that remains unchanged?, I want Unique device ID that cannot be changed if I uninstall application and install again on same device. I am Using swift 2.1.1 and xcode 7.2.1. I have tried this code but both will not work.

我想要 iPhone 的唯一设备 ID 保持不变?,我想要唯一设备 ID,如果我卸载应用程序并在同一设备上重新安装,则无法更改。我正在使用 swift 2.1.1 和 xcode 7.2.1。我试过这段代码,但两者都不起作用。

//First approach

//第一种方法

let device_id = UIDevice.currentDevice().identifierForVendor!.UUIDString
print("unique device id: (device_id)")

let device_id = UIDevice.currentDevice().identifierForVendor!.UUIDString
print("唯一设备ID:(device_id)")

//Second approach

//第二种方法

let dID = CFUUIDCreate(kCFAllocatorDefault)
let deviceID = CFUUIDCreateString(kCFAllocatorDefault, dID) as NSString
print("unique CFUUID id: (deviceID)")

let dID = CFUUIDCreate(kCFAllocatorDefault)
let deviceID = CFUUIDCreateString(kCFAllocatorDefault, dID) as NSString
print("unique CFUUID id: (deviceID)")

回答by SwiftDeveloper

Swift 3.XLatest simple usage;

Swift 3.X最新的简单用法;

if let identifierForVendor = UIDevice.current.identifierForVendor {
    print(identifierForVendor.uuidString)
}

回答by thexande

This is working in swift 3 / Xcode / Iphone7 for me.

这对我来说在 swift 3 / Xcode / Iphone7 中有效。

let deviceUUID: String = (UIDevice.current.identifierForVendor?.uuidString)!

回答by Badal Shah

Try this ,

尝试这个 ,

UIDevice.currentDevice().identifierForVendor

or if you want a string:

或者如果你想要一个字符串:

UIDevice.currentDevice().identifierForVendor.UUIDString
print(UIDevice.currentDevice().identifierForVendor().UUIDString()) //Print Log

回答by Avinash651

Use this

用这个

let UUID = CFUUIDCreateString(nil, CFUUIDCreate(nil))
 func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {

let userDefaults = NSUserDefaults.standardUserDefaults()

if userDefaults.objectForKey("ApplicationUniqueIdentifier") == nil {
    let UUID = NSUUID.UUID().UUIDString
    userDefaults.setObject(UUID, forKey: "ApplicationUniqueIdentifier")
    userDefaults.synchronize()
}
return true

}

}

This will work correctly in swift

这将在 swift 中正常工作

回答by Yagnesh Dobariya

print( UIDevice.currentDevice().identifierForVendor().UUIDString())