Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
398 views
in Technique[技术] by (71.8m points)

iphone - Always get a unique device id in iOS 7

Our iOS application is for specific users. So, we used device unique identifier for user identification. This approach works fine till iOS 6, because we are getting the same value every time.

NSString *strUniqueIdentifier = [[UIDevice currentDevice] uniqueIdentifier];

In iOS 7, the above method is returning different values and we are getting issues in user identification. iOS 7 provides the following alternate.

NSUUID *oNSUUID = [[UIDevice currentDevice] identifierForVendor];
[strApplicationUUID setString:[oNSUUID UUIDString]];

We replaced uniqueIdentifier with identifierForVendor, and created an Ad-hoc build. We then installed the build on both iOS 7 and iOS 6 devices. So far in iOS 7, we are getting the same value every time, but iOS 6 gives different values every time we delete and reinstall the app.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use this little helper method to keep identifier in Keychain between install/delete sessions of app

-(NSString *)getUniqueDeviceIdentifierAsString
{
    NSString *appName=[[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString*)kCFBundleNameKey];

    NSString *strApplicationUUID = [SSKeychain passwordForService:appName account:@"incoding"];
    if (strApplicationUUID == nil)
    {
        strApplicationUUID  = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
        [SSKeychain setPassword:strApplicationUUID forService:appName account:@"incoding"];
    }

    return strApplicationUUID;
}

Add the SSKeychain library to your project, e.g. via Cocoapods with pod 'SSKeychain'


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...