获取IOS唯一设备标识码
最开始使用的是苹果官方KeychainItemWrapper,下载地址为https://developer.apple.com/library/ios/samplecode/GenericKeychain/Listings/Classes_KeychainItemWrapper_m.html,使用过程中,会有报错*** Terminating app due to uncaught exception
'NSInternalInconsistencyException', reason: 'Couldn't add the Keychain Item.',在网上没有找到合适的解决方法,最终放弃这种方式
现在使用的是SSKeyChains,下载地址为项目地址:https://github.com/samsoffes/sskeychain
步骤:
- 在工程中加入Security.framework框架。
- 把SSKeychain.h和SSKeychain.m加到项目文件夹。
- 获取UUID(使用了ARC模式,非ARC模式的代码文件加入 -fno-objc-arc标签)
UUID.h
#import
<Foundation/Foundation.h>
@interface
UUID : NSObject
+
(NSString *) gen_uuid;
@end
|
UUID.m
#import
"UUID.h"
@implementation
UUID
+(NSString
*) gen_uuid
{
CFUUIDRef uuid_ref=CFUUIDCreate(nil);
CFStringRef uuid_string_ref=CFUUIDCreateString(nil,
uuid_ref);
CFRelease(uuid_ref);
NSString *uuid=[NSString stringWithString:uuid_string_ref];
CFRelease(uuid_string_ref);
return uuid;
}
@end
|
- 获取UUID,保存在钥匙串中(蓝色部分为标识,最好为app唯一标识,通过此标识查找保存的钥匙串)
#define
kService [NSBundle mainBundle].bundleIdentifier
#define kAccount @"K626P85663.com.tzd.mofi2"
|
//获取设备唯一码
if (![SSKeychain passwordForService:kService account:kAccount])
{
NSString *uuid = [UUID gen_uuid];
[SSKeychain setPassword:uuid
forService:kService account:kAccount];
}
NSString
*devicenumber = [SSKeychain passwordForService:kService account:kAccount];
|
注意(已下全部验证):
(1)
app卸载重新安装,保存的唯一码不改变
(2)
IOS系统升级,保存的唯一码不改变
(3)
通用—>还原—>还原所有设置,保存的唯一码不改变
(4) 通用—>还原—>抹掉所有内容和设置,保存的唯一码也被抹掉
|