UIDevice uniqueIdentifier deprecated - What to do now?

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for 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.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my