How to generate unique identifier which should work in all iOS versions?

前端 未结 3 572
南旧
南旧 2020-12-02 15:00

I want to get the unique identifier which should support all iOS versions..Can any one help me on this issue. As you know that apple is deprecated the UDID method, So there

相关标签:
3条回答
  • 2020-12-02 15:17

    Now Device Identifier change to UUID.You can get UUID With the help of following code:

    - (NSString *)getUUID
    {
        NSString *UUID = [[NSUserDefaults standardUserDefaults] objectForKey:@"uniqueID"];
        if (!UUID) {
            CFUUIDRef theUUID = CFUUIDCreate(NULL);
            CFStringRef string = CFUUIDCreateString(NULL, theUUID);
            CFRelease(theUUID);
            UUID = [(__bridge NSString*)string stringByReplacingOccurrencesOfString:@"-"withString:@""];
            [[NSUserDefaults standardUserDefaults] setValue:UUID forKey:@"uniqueID"];
        }
        return UUID;
    }
    

    It's Work in all iOS version.

    0 讨论(0)
  • 2020-12-02 15:39

    I was updating my application that was working based only on Unique Identifier which supported iOS 4.3 and above. So,

    1) I was unable to use [UIDevice currentDevice].uniqueIdentifier; as it was no longer available

    2) I could not use [UIDevice currentDevice].identifierForVendor.UUIDString because it was Available in iOS 6.0 and later only and was unable to use for lower iOS versions.

    3) The mac address was not an option as it wasn't allowed in iOS-7

    4) OpenUDID was deprecated some time ago and also had issues with iOS-6.

    5) Advertisement identifiers were also not available for iOS-5 and below

    Finally this was what i did

    a) Added SFHFKeychainUtils to the project

    b) Generated CFUUID key String

     CFUUIDRef cfuuid = CFUUIDCreate(kCFAllocatorDefault);
        udidString = (NSString*)CFBridgingRelease(CFUUIDCreateString(kCFAllocatorDefault, cfuuid));
    

    c) Saved it to Key Chain Utils or else it will generate a new Unique Each Time

    Final Code

    + (NSString *)GetDeviceID {
        NSString *udidString;
       udidString = [self objectForKey:@"deviceID"];
        if(!udidString)
        {
        CFUUIDRef cfuuid = CFUUIDCreate(kCFAllocatorDefault);
        udidString = (NSString*)CFBridgingRelease(CFUUIDCreateString(kCFAllocatorDefault, cfuuid));
        CFRelease(cfuuid);
            [self setObject:udidString forKey:@"deviceID"];
        }
        return udidString;
    }
    
    +(void) setObject:(NSString*) object forKey:(NSString*) key
    {
        NSString *objectString = object;
        NSError *error = nil;
        [SFHFKeychainUtils storeUsername:key
                             andPassword:objectString
                          forServiceName:@"LIB"
                          updateExisting:YES
                                   error:&error];
    
        if(error)
            NSLog(@"%@", [error localizedDescription]);
    }
    
    +(NSString*) objectForKey:(NSString*) key
    {
        NSError *error = nil;
        NSString *object = [SFHFKeychainUtils getPasswordForUsername:key
                                                      andServiceName:@"LIB"
                                                               error:&error];
        if(error)
            NSLog(@"%@", [error localizedDescription]);
    
        return object;
    }
    

    enter image description here

    For further Details

    0 讨论(0)
  • 2020-12-02 15:44

    I don't have access to the code right now (can post in a few hours if you still need it) but what I've done is create a static method 'deviceIdentifier' in a helper class.

    the method does a basic check for the current iOS version, returns UDID if below 6.0 and uniqueIdentifier otherwise

    Let me know if you'd like the code for that and I'll post it when I can..it's only 10-15 lines or so if I remember right but makes a big difference as then you can just call '[myHelper deviceIdentifier]' wherever you need a device ID and not have to worry about which iOS version they are using

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