1.首先引入第三方库:KeychainItemWrapper,下载地址:http://pan.baidu.com/s/1eRdEGSi
2.把KeychainItemWrapper.h和KeychainItemWrapper.m文件拉入工程,还要添加Security.framework;
3.由于这个第三方库是MRC格式下的库,所以需要做一些兼容ARC格式的配置:target--》Build Phases--》Compile Sources,添加-fno-objc-arc
设置如图
4.把KeychainItemWrapper拉入工程并建立获取UUID的帮助类(继承NSObject的类)效果如图:
5.帮助类UUIDHelp里添加如下代码:
UUIDHelp.h里面
+ (NSString *)getUUIDNumber;
UUIDHelp.m里面
首先加入头文件
#import "KeychainItemWrapper.h"
添加代码
+ (NSString *)getUUIDNumber{ NSString *key = @"com.app.keychain.uuid"; KeychainItemWrapper *keychainItem = [[KeychainItemWrapper alloc] initWithIdentifier:key accessGroup:nil]; NSString *strUUID = [keychainItem objectForKey:(__bridge id)kSecValueData]; if (strUUID.length <= 0) { strUUID = [[[UIDevice currentDevice] identifierForVendor] UUIDString]; [keychainItem setObject:@"uuid" forKey:(__bridge id)kSecAttrAccount]; [keychainItem setObject:strUUID forKey:(__bridge id)kSecValueData]; } return strUUID; }
6.直接在要获取手机序列号的类里直接回去就可以了:
NSString *UUIDStr = [UUIDHelp getUUIDNumber];
大功告成!!!!