UIDevice uniqueIdentifier deprecated - What to do now?


📱 UIDevice uniqueIdentifier deprecated - What to do now?
So you just found out that the uniqueIdentifier
property in UIDevice
is deprecated in iOS 5 and unavailable in iOS 7 and above. Yikes! It's understandable if you're feeling a bit lost and looking for some guidance on how to handle this issue.
The Problem 😱
The uniqueIdentifier
property was often used by developers to uniquely identify a device. It was a reliable way to track installations or provide certain features within an app. However, relying on this property is no longer an option due to its deprecation.
Easy Solutions 💡
You might be wondering, "What can I do now?" Don't worry, we've got you covered with some easy solutions:
Solution 1: CFUUIDCreate 👨💻
One suggestion from the Apple documentation in 2011-2012 was to use the CFUUIDCreate
function to generate a unique identifier specific to your app. You can then store this identifier in the NSUserDefaults
database. Here's an example of how you could implement it:
let uniqueIdentifier = CFUUIDCreateString(nil, CFUUIDCreate(nil)) as String
UserDefaults.standard.set(uniqueIdentifier, forKey: "UniqueIdentifier")
That looks simple enough, right? However, there's one catch. If a user uninstalls and re-installs your app, the identifier will change. So, this solution may not work for scenarios where you need a persistent identifier.
Solution 2: Keychain Services 🔒
If you require a persistent identifier that remains the same even if the user uninstalls and re-installs your app, you can use Keychain Services. Keychain Services provide secure storage for small pieces of data, including identifiers. Here's an example of how you could implement it:
import Security
func getPersistentIdentifier() -> String? {
let serviceName = Bundle.main.bundleIdentifier ?? "defaultService"
let query: [String: Any] = [
kSecClass as String: kSecClassGenericPassword,
kSecAttrService as String: serviceName,
kSecAttrAccount as String: "PersistentIdentifier",
kSecMatchLimit as String: kSecMatchLimitOne,
kSecReturnData as String: true
]
var result: AnyObject?
let status = SecItemCopyMatching(query as CFDictionary, &result)
guard status == errSecSuccess,
let persistentIdentifierData = result as? Data,
let persistentIdentifier = String(data: persistentIdentifierData, encoding: .utf8) else {
return nil
}
return persistentIdentifier
}
With this solution, even if a user uninstalls and re-installs your app, you can retrieve the same persistent identifier.
Call-to-action 🚀
Now that you have a couple of solutions in your toolkit, it's time to tackle this problem head-on. Choose the solution that best fits your app's requirements and get to work on implementing it.
Remember to test thoroughly and consider any edge cases specific to your app. And if you come up with any other creative solutions, feel free to share them in the comments below. Let's help each other navigate through these challenges!
👉 Have you faced issues with uniqueIdentifier
deprecation? Share your experience and the solution you implemented in the comments below! 🗣
Happy coding! 💻✨
Take Your Tech Career to the Next Level
Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.
