ios7 uuid的获取方法

ios7后mac地址沦为鸡肋,所以必须得重新想办法获取设备的id信息,apple推荐用UUID,但app重新安装后,UUID需要重设,所以想到把UUID存储到ios系统的keychain中,既然存储在系统内,也就意味着即使app被删除也不会丢失该属性。

//ios获取uuid

#import "KeychainItemWrapper.h"

KeychainItemWrapper *keychainItem = [[KeychainItemWrapperalloc]

initWithIdentifier:@"UUID"

accessGroup:@"com.game.userinfo"];

NSString *strUUID = [keychainItem objectForKey:(id)kSecValueData];

if ([strUUID isEqualToString:@""])

{

CFUUIDRef uuidRef = CFUUIDCreate(kCFAllocatorDefault);

strUUID = (NSString *)CFUUIDCreateString (kCFAllocatorDefault,uuidRef);

[keychainItem setObject:strUUID forKey:(id)kSecValueData];

}

[keychainItem release];

std::string device_uuid = [strUUID UTF8String];

ps:注意引入 Security.framework 库!上面的KeychainItemWrapper类从以下地址下载

https://developer.apple.com/library/ios/samplecode/GenericKeychain/Listings/Classes_KeychainItemWrapper_h.html#//apple_ref/doc/uid/DTS40007797-Classes_KeychainItemWrapper_h-DontLinkElementID_9

除了上面的方法,还有一个轻量级的方法,推荐使用这个,使用超级简单,而且完全开源

首先去系统取“uuid”字段,如果有直接使用,如果没有,则create一个uuid并存入keychain中

NSString *retrieveuuid = [SSKeychainpasswordForService:@"com.game.userinfo"account:@"uuid"];

if ( retrieveuuid == nil || [retrieveuuid isEqualToString:@""])

{

CFUUIDRef uuid = CFUUIDCreate(NULL);

assert(uuid != NULL);

CFStringRef uuidStr = CFUUIDCreateString(NULL, uuid);

retrieveuuid = [NSString stringWithFormat:@"%@", uuidStr];

[SSKeychain setPassword: retrieveuuid

forService:@"com.game.userinfo"account:@"uuid"];

}

注意导入

SSKeychain这个类,可以去http://github.com/soffes/sskeychain上下载代码文件,good luck!

上一篇:创建Maven项目


下一篇:OBD K线抓包