简体   繁体   English

如何在iOS中删除并重新安装的应用程序中唯一标识设备?

[英]How to uniquely identify a device even the app deleted and reinstalled in iOS?

My app has connected to remote push notification service. 我的应用已连接到远程推送通知服务。 In server side they want to keep a unique ID for the device and it should not change even the app has deleted and re installed. 在服务器端,他们希望为设备保留唯一的ID,即使应用已删除并重新安装,ID也不应更改。 Can we get Device UDID and send to the server? 我们可以获取设备UDID并将其发送到服务器吗? And If I did it will Apple reject my app from the store? 如果我做到了,苹果会否拒绝商店中的我的应用程序? And I think that is the best ID to keep permanantly in the server side even the app deleted.? 而且我认为即使删除应用程序,这也是永久保留在服务器端的最佳ID。

I need your suggestions. 我需要你的建议。 Please help me. 请帮我。 Thanks 谢谢

Here some tests I made using identifierForVendor on iOS7 这是我在iOS7上使用identifierForVendor进行的一些测试

After app installation: 28FD42B6-A993-4602-A988-69E375A1F913 应用安装后:28FD42B6-A993-4602-A988-69E375A1F913
After killing the app: 28FD42B6-A993-4602-A988-69E375A1F913 终止应用后:28FD42B6-A993-4602-A988-69E375A1F913
After deleting and reinstalling the app: 28FD42B6-A993-4602-A988-69E375A1F913 删除并重新安装应用程序后:28FD42B6-A993-4602-A988-69E375A1F913
After system restore and reinstalling the app: 4948F77F-3D41-4933-B2F0-C4DCB529C7CC 系统还原并重新安装应用程序后:4948F77F-3D41-4933-B2F0-C4DCB529C7CC
After restore from backup made before system restore: 28FD42B6-A993-4602-A988-69E375A1F913 从系统还原之前进行的备份还原之后:28FD42B6-A993-4602-A988-69E375A1F913

So the identifier for vender persists even after deleting and reinstalling the app only if there are application of the same vendor on the device. 因此, 只有在设备上存在相同供应商的应用程序时,即使在删除并重新安装该应用程序后,供应商标识符仍会保留

The value in this property remains the same while the app (or another app from the same vendor) is installed on the iOS device. 当该应用程序(或同一供应商的另一个应用程序)安装在iOS设备上时,此属性中的值保持不变。 The value changes when the user deletes all of that vendor's apps from the device and subsequently reinstalls one or more of them. 当用户从设备中删除该供应商的所有应用程序并随后重新安装其中一个或多个应用程序时,该值将更改。 Therefore, if your app stores the value of this property anywhere, you should gracefully handle situations where the identifier changes. 因此,如果您的应用将此属性的值存储在任何位置,则应妥善处理标识符更改的情况。

The APNS token it should be used only as an identifier for sending push, it should not be related to a specific device by any means. APNS令牌仅应用作发送推送的标识符,不应以任何方式与特定设备相关。 Push tokens can change over time and you should de ready to manage that, this is stated in Apple documentation (even if I never see one changes even after disabling and reenabling them, it seems that it changes after iOS updates ). 推送令牌会随着时间而变化,您应该准备好对此进行管理,这在Apple文档中有说明(即使禁用并重新启用推送令牌后,即使我从未见过任何更改,它似乎在iOS更新后也会更改)。
Another is creating your UUID and saving it in the keychain, this will only be deleted only after a system restore. 另一个方法是创建您的UUID并将其保存在钥匙串中,只有在系统还原后才会将其删除。
If you want to save it in NSUserDefault be aware that it is deleted with your app. 如果要将其保存在NSUserDefault中,请注意它已随您的应用程序一起删除。

You can find interesting this post from NSHipster. 您可以从NSHipster中找到有趣的帖子

  • Keychain will reset if when you reset your device. 如果您重置设备,钥匙串将重置。

  • Unique id from "[[UIDevice currentDevice] identifierForVendor]" will change every time when you delete and re-install the app. 每次删除并重新安装应用程序时,“ [[[UIDevice currentDevice] identifierForVendor]”中的唯一ID都会更改。

You are right, you should use the device token from APNS for unique id. 没错,您应该使用APNS中的设备令牌作为唯一ID。

As its mentioned ; 如所提到的; When app is installed its uuid is unique but if app deleted and installed again its uuid changes. 安装应用程序后,其uuid是唯一的,但是如果删除并重新安装应用程序,则其uuid会更改。

So overcome this issue I used Keychain Access to retrieval and saving uuid. 因此,克服了这个问题,我使用钥匙串访问来检索和保存uuid。

For keychain access wrapper i used a third party named 对于钥匙串访问包装器,我使用了一个名为

pod 'SwiftKeychainWrapper'

and for getting uuid I created this method. 为了获得uuid,我创建了此方法。

func getUDIDofDevice() -> String {
    //Check is uuid present in keychain or not
    if let deviceId: String = KeychainWrapper.standard.string(forKey: "deviceId") {
        return deviceId
    }else{
        // if uuid is not present then get it and store it into keychain
        let key : String = (UIDevice.current.identifierForVendor?.uuidString)!
        let saveSuccessful: Bool = KeychainWrapper.standard.set(key, forKey: "deviceId")
        print("Save was successful: \(saveSuccessful)")
        return key
    }
}

What will happen here : if in keychain access uuid is not present then by using uuid string save it. 这里会发生什么:如果在钥匙串访问中不存在uuid,则使用uuid字符串保存它。 and whenever you required this uuid it will be fetched from keychain access. 并且每当您需要此uuid时,它将从钥匙串访问中获取。 Even if app is deleted / replaced its unique. 即使应用程序被删除/替换了它的独特性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM