我发布的应用程序利用CFUUID
和SSKeychain
来识别设备(即使卸载并重新安装应用程序,也保持该ID不变)。
我将这些设备ID保存在服务器上,并最近发现一些用户在同一台真实设备上有几个这样的ID。我唯一能想到的解释是ID未从钥匙链中保存或加载,因此设备会生成一个新的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
并没有按预期工作。在应用程序的后面部分,我注册推送通知,并将推送令牌与此CFUUID一起发送到服务器,使用完全相同的代码行[SSKeychain passwordForService:serviceName account:identifier]
读取,但是当它发送到服务器时,它不是nil(因此我可以看到几个具有相同推送令牌的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方法只是将数据发送到服务器,然后回调委托,这部分工作正常。我可以在服务器上看到接收此命令的日志:
"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"
}
]
我看到导致奇怪行为的可能是两个因素,控制器主体中的 NSStrings
和静态的 getDevice
方法。然而,我不明白为什么这在许多设备上可以运行但在其他设备上却失败了。