ios - 从同一部 iPhone 获取两个不同的设备 ID

标签 ios iphone swift3 udid

当我得到它时,我的 iphone 得到了不同的 UDID from itunes像这样以编程方式

UDID:String = UIDevice.current.identifierForVendor!.uuidString

基本上我试图为我的 iphone 获取一个唯一标识符,就像我们有 android 手机的 mac 地址一样。

最佳答案

一种最简单的方法是通过将 identifierForVendor 存储在钥匙串(keychain)中来解决此问题。即使您卸载应用程序, key 的值也保持不变。许多第三方库可用于执行此操作。其中之一https://github.com/jrendel/SwiftKeychainWrapper .

func getGlobalUniqueIdentifierFromKeyChain()->String{

    let retrievedString: String? = KeychainWrapper.standard.string(forKey: "DeviceId")

    if  retrievedString == nil{
        if let deviceKey  = UIDevice.current.identifierForVendor?.uuidString{
            let _ = KeychainWrapper.standard.set(deviceKey, forKey: "DeviceId")
        }
    }

    if let globalID = KeychainWrapper.standard.string(forKey: "DeviceId"){
        return globalID
    }else{
        return UIDevice.current.identifierForVendor?.uuidString ?? ""
    }
}

关于ios - 从同一部 iPhone 获取两个不同的设备 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47858394/

相关文章:

ios - 使用 UITableViewCell 作为 tableHeaderView

ios - 我遇到这样的错误,我该如何解决一个变量'b'从未被突变;考虑更改为“let”常量

iphone - 如何使用 AVAudioRecorder 恢复录音?

iphone - 嵌套的 UIScrollViews 同时滚动

ios - 警告 : Attempt to present vc whose view is not in the window hierarchy

iphone - 在 UICollectionView 中相互连接的单元格

iphone - 如何在另一个 View Controller 中调用 NSNotificationCenter 函数?

iphone - 在 Swift 中以编程方式将类添加到 UIbutton

ios - Swift异步加载报错

ios - 单击文本字段以从 DatePickerDialog 获取日期时键盘卡住