IOS App生成用户的唯一标示
苹果禁止直接使用设备的UUID,推荐使用CFUUID生成用户的唯一标示,代码如下:
const char* function getUUID(){
NSString *uuid = [[NSUserDefaults standardUserDefaults] objectForKey:@"me.ouyang.testapp"];
if (uuid) {
}else{
CFUUIDRef uuidRef = CFUUIDCreate(NULL);
CFStringRef stringRef = CFUUIDCreateString(NULL, uuidRef);
CFRelease(uuidRef);
uuid = (NSString*)stringRef;
[[NSUserDefaults standardUserDefaults] setObject:uuid forKey:@"me.ouyang.testapp"];
}
return [uuid UTF8String];
}
如果用户重新安装应用或者卸载了应用,上面生成的UUID就会丢失。
可以把uuid存入KeyChain,只要用户不重装系统,那么这个UUID都会一直存在。写入KeyChain需要借助SSKeychain
const char* function getUUID(){
NSError *error = nil;
NSString *uuid = [SSKeychain passwordForService:@"me.ouyang.testapp" account:@"uuid" error:&error];
if(uuid){
}else {
CFUUIDRef uuidRef = CFUUIDCreate(NULL);
CFStringRef stringRef = CFUUIDCreateString(NULL, uuidRef);
CFRelease(uuidRef);
uuid = (NSString*)stringRef;
[SSKeychain setPassword:uuid forService:@"me.ouyang.testapp" account:@"uuid"];
}
return [uuid UTF8String];
}
Last modified on 2012-06-29