ios - 存储在 SSKeychain 中的 CFUUID 在某些设备中为空

标签 ios objective-c keychain sskeychain

我发布的应用程序使用 CFUUIDSSKeychain 来识别设备(并保持该 ID 不变,即使应用程序被卸载并重新安装)

我将这些设备 ID 保存在服务器中,最近我注意到一些用户拥有同一个真实设备的多个 ID。我看到的唯一解释是 ID 没有被保存或从 Keychain 加载,因此设备生成了一个新 ID。奇怪的是它在运行相同 iOS 版本的其他一些设备上运行良好。

对可能发生的事情有什么想法吗?

这是我在 (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

中的相关代码
NSString* identifier = @"appName";
NSString* serviceName = @"com.company.appName";
NSString *retrieveuuid = [SSKeychain passwordForService:serviceName account:identifier];
if (retrieveuuid == nil) {
    CFUUIDRef theUUID = CFUUIDCreate(NULL);
    CFStringRef string = CFUUIDCreateString(NULL, theUUID);
    CFRelease(theUUID);
    NSString *uuid  = (NSString*) string;
    [SSKeychain setPassword:uuid forService:serviceName account:identifier];
}

编辑:我的猜测是 retrieveuuid == nil 由于某种原因没有按预期工作。稍后在应用程序中,我注册了推送通知,并将推送 token 连同我用完全相同的行 [SSKeychain passwordForService:serviceName account:identifier] 读取的这个 CFUUID 一起发送到服务器,但是当它被发送时到服务器它不是零(所以我可以看到几个具有相同推送 token 的 CFUUID)。

编辑 2 以附加更多实际代码。

AppDelegate.m

NSString* identifier = @"appName";
NSString* serviceName = @"com.company.appName";
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //Creating UUID
    NSString *retrieveuuid = [AppDelegate getDeviceId];
    if (retrieveuuid == nil) {
        CFUUIDRef theUUID = CFUUIDCreate(NULL);
        CFStringRef string = CFUUIDCreateString(NULL, theUUID);
        CFRelease(theUUID);
        NSString *uuid  = (NSString*) string;
        [SSKeychain setPassword:uuid forService:serviceName account:identifier];
    }

    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
     (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
}

+ (NSString*) getDeviceId {
    return [SSKeychain passwordForService:serviceName account:identifier];
}

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
    NSString *newToken = [deviceToken description];
    newToken = [newToken stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
    newToken = [newToken stringByReplacingOccurrencesOfString:@" " withString:@""];

    _deviceToken = [[NSString alloc] initWithString:newToken];

    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    NSString *user = [prefs objectForKey:@"appNameUsername"];

    if(user && ![user isEqualToString:@""]){
        RestClient *rest = [[RestClient alloc] init];
        rest.delegate = self;
        rest.tag = 2;
        [rest updateToken:newToken ForUsername:user];
        [rest release];
    }
}

RestClient.m

-(void) updateToken:(NSString *)token ForUsername:(NSString *)userName{
    NSArray* info = [NSArray arrayWithObject: [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                               userName, @"Username",
                                               token, @"TokenNo",
                                               [[UIDevice currentDevice].model hasPrefix:@"iPad"] ? @"iPad" : @"iPhone", @"Device",
                                               [UIDevice currentDevice].systemVersion, @"OSVersion",
                                               [AppDelegate getDeviceId], @"DeviceID",
                                               @"updateToken", @"CMD",
                                               nil]];
    [self doAction:info];
}

doAction 方法只是将数据发送到服务器,然后回调委托(delegate),这部分工作正常。我可以在服务器上看到接收此命令的日志:

"JSONCMD":"[
{   "TokenNo" : "f0d3aa21758350333b7e6315c38_EDIT_257c1838f49c43049f8380ec1ff63", 
    "AppVersion" : "1.0.4",
    "Username" : "user@server.com", 
    "CMD" : "updateToken", 
    "OSVersion" : "7.0.4", 
    "DeviceID" : "9B794E11-6EF7-470C-B319-5A9FCCDAFD2B", 
    "Device" : "iPhone"
}
]  

我看到 2 个可能的候选者导致了奇怪的行为, Controller 主体中的 NSStrings 和静态 getDevice 方法。但是,我看不出这在许多设备上如何工作但在其他设备上却失败了。

最佳答案

我遇到了同样的问题,并且找到了解决方案。问题发生在用户设置了密码以解锁手机的设备上。在 iOS7 上,某些代码可以在后台运行(推送通知、后台获取),如果您将某些内容保存在具有标准辅助功能类型的钥匙串(keychain)中,则当设备锁定时(应用程序在后台运行),您将无法读取它。尝试这样设置:

[SSKeychain setAccessibilityType:kSecAttrAccessibleAlways];

在读/写钥匙串(keychain)之前。 希望对您有所帮助。

关于ios - 存储在 SSKeychain 中的 CFUUID 在某些设备中为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20759169/

相关文章:

ios - 将NSInteger添加到NSMutableArray

objective-c - 以编程方式获取 macOS 上的硬盘驱动器信息

ios - iOS 钥匙串(keychain)使用限制

ios - Sierra : Keychain ignores access control settings and UI-prompts for permission 中的安全/代码设计

iphone - 使用 iphone sdk 保存钥匙串(keychain)时出错

ios - 在 for 循环中创建的 UIButtons 的特定位置

iphone - 在 PopOverController 的帮助下填充表 - Objective C

ios - 如何在 Xcode 6.4 的 Interface Builder 中设置负对齐中心 X 或 Y 值?

ios - 想结合使用 TouchID 和电子邮件来保护我的应用程序

ios - 导航栏在动画期间不反射(reflect)新的文本属性到以前的 View Controller