UIDevice uniqueIdentifier deprecated - What to do now?

后端 未结 30 2104
日久生厌
日久生厌 2020-11-21 06:07

It has just come to light that the UIDevice uniqueIdentifier property is deprecated in iOS 5 and unavailable in iOS 7 and above. No alternative method or pr

相关标签:
30条回答
  • 2020-11-21 06:32

    A UUID created by CFUUIDCreate is unique if a user uninstalls and re-installs the app: you will get a new one each time.

    But you might want it to be not unique, i. e. it should stay the same when the user uninstalls and re-installs the app. This requires a bit of effort, since the most reliable per-device-identifier seems to be the MAC address. You could query the MAC and use that as UUID.

    Edit: One needs to always query the MAC of the same interface, of course. I guess the best bet is with en0. The MAC is always present, even if the interface has no IP/is down.

    Edit 2: As was pointed out by others, the preferred solution since iOS 6 is -[UIDevice identifierForVendor]. In most cases, you should be able use it as a drop-in replacement to the old -[UIDevice uniqueIdentifier] (but a UUID that is created when the app starts for the first time is what Apple seems to want you to use).

    Edit 3: So this major point doesn't get lost in the comment noise: do not use the MAC as UUID, create a hash using the MAC. That hash will always create the same result every time, even across reinstalls and apps (if the hashing is done in the same way). Anyways, nowadays (2013) this isn't necessary any more except if you need a "stable" device identifier on iOS < 6.0.

    Edit 4: In iOS 7, Apple now always returns a fixed value when querying the MAC to specifically thwart the MAC as base for an ID scheme. So you now really should use -[UIDevice identifierForVendor] or create a per-install UUID.

    0 讨论(0)
  • 2020-11-21 06:32

    A not perfect but one of the best and closest alternative to UDID (in Swift using iOS 8.1 and Xcode 6.1):

    Generating a random UUID

    let strUUID: String = NSUUID().UUIDString
    

    And use KeychainWrapper library:

    Add a string value to keychain:

    let saveSuccessful: Bool = KeychainWrapper.setString("Some String", forKey: "myKey")
    

    Retrieve a string value from keychain:

    let retrievedString: String? = KeychainWrapper.stringForKey("myKey")
    

    Remove a string value from keychain:

    let removeSuccessful: Bool = KeychainWrapper.removeObjectForKey("myKey")
    

    This solution uses the keychain, thus the record stored in the keychain will be persisted, even after the app is uninstalled and reinstalled. The only way of deleting this record is to Reset all contents and settings of the device. That is why I mentioned that this solution of substitution is not perfect but stays one of the best solution of replacement for UDID on iOS 8.1 using Swift.

    0 讨论(0)
  • 2020-11-21 06:33

    May help: use below code it will always Unique except you erase(Format) your device.

    UIDevice *myDevice=[UIDevice currentDevice];
    NSString *UUID = [[myDevice identifierForVendor] UUIDString];
    
    0 讨论(0)
  • 2020-11-21 06:39

    Based on the link proposed by @moonlight, i did several tests and it seems to be the best solution. As @DarkDust says the method goes to check en0 which is always available.
    There are 2 options:
    uniqueDeviceIdentifier (MD5 of MAC+CFBundleIdentifier)
    and uniqueGlobalDeviceIdentifier(MD5 of the MAC), these always returns the same values.
    Below the tests i've done (with the real device):

    #import "UIDevice+IdentifierAddition.h"
    
    NSLog(@"%@",[[UIDevice currentDevice] uniqueDeviceIdentifier]);
    NSLog(@"%@",[[UIDevice currentDevice] uniqueGlobalDeviceIdentifier]);
    

    XXXX21f1f19edff198e2a2356bf4XXXX - (WIFI)UDID
    XXXX7dc3c577446a2bcbd77935bdXXXX - (WIFI)GlobalAppUDID

    XXXX21f1f19edff198e2a2356bf4XXXX - (3G)UDID
    XXXX7dc3c577446a2bcbd77935bdXXXX - (3G)GlobalAppUDID

    XXXX21f1f19edff198e2a2356bf4XXXX - (GPRS)UDID
    XXXX7dc3c577446a2bcbd77935bdXXXX - (GPRS)GlobalAppUDID

    XXXX21f1f19edff198e2a2356bf4XXXX - (AirPlane mode)UDID
    XXXX7dc3c577446a2bcbd77935bdXXXX - (AirPlane mode)GlobalAppUDID

    XXXX21f1f19edff198e2a2356bf4XXXX - (Wi-Fi)after removing and reinstalling the app XXXX7dc3c577446a2bcbd77935bdXXXX (Wi-Fi) after removing and installing the app

    Hope it's useful.

    EDIT:
    As others pointed out, this solution in iOS 7 is no longer useful since uniqueIdentifier is no longer available and querying for MAC address now returns always 02:00:00:00:00:00

    0 讨论(0)
  • 2020-11-21 06:40

    Perhaps you can use:

    [UIDevice currentDevice].identifierForVendor.UUIDString
    

    Apple's documentation describes identifierForVender as follows:

    The value of this property is the same for apps that come from the same vendor running on the same device. A different value is returned for apps on the same device that come from different vendors, and for apps on different devices regardless of vendor.

    0 讨论(0)
  • 2020-11-21 06:42

    You can use

    NSString *sID = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
    

    Which is unique for the device in all application.

    0 讨论(0)
提交回复
热议问题