始终在iOS 7中获取唯一的设备ID [英] Always get a unique device id in iOS 7

查看:225
本文介绍了始终在iOS 7中获取唯一的设备ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的iOS应用程序适用于特定用户。因此,我们使用设备唯一标识符进行用户识别。这种方法适用于iOS 6,因为我们每次都获得相同的值

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];

在iOS 7中,上述方法返回不同的值,我们是在用户识别中遇到问题。 iOS 7提供以下备用

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]];

我们用<$ c $替换了 uniqueIdentifier c> identifierForVendor ,并创建了一个Ad-hoc构建。然后,我们在iOS 7和iOS 6设备上安装了构建版本。到目前为止,在iOS 7中,我们每次都获得相同的值,但每次删除并重新安装应用时,iOS 6都会提供不同的值

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.

推荐答案

使用这个小助手方法将密钥链中的标识符保存在app的安装/删除会话之间

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;
}

添加 SSKeychain 库到您的项目,例如通过Cocoapods与 pod'SSKeychain'

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

这篇关于始终在iOS 7中获取唯一的设备ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆