前段時間后臺做統(tǒng)計功能的時候需要用到設(shè)備的唯一標(biāo)識练慕,所以得客戶端這邊獲取設(shè)備的唯一標(biāo)識并將其上傳到服務(wù)器赃绊。對于iOS,說到設(shè)備的唯一標(biāo)識以现,估計大家都會不由自主的想到UUID。那該如何去獲取設(shè)備的UUID呢?其實很簡單约啊,一行代碼即可獲取
[[UIDevice currentDevice] identifierForVendor] UUIDString]
.
但是這樣的話存在一定的弊端邑遏,當(dāng)你升級或者重裝軟件的時候,UUID會發(fā)生變化恰矩,這時候我們再去獲取UUID時记盒,得到的值和以前的不一樣,但其實還是同一臺設(shè)備外傅,這樣的話會造成我們的統(tǒng)計數(shù)據(jù)不準(zhǔn)纪吮。所以說我們在獲取到UUID后需要解決的是如何保證它的唯一性俩檬。
keychain顧名思義,是鑰匙串的意思碾盟,是蘋果公司Mac OS中的密碼管理系統(tǒng)棚辽。keychain里保存的信息不會因App被刪除而丟失,在用戶重新安裝App后依然有效巷疼,數(shù)據(jù)還在晚胡。所以我們可以將UUID存儲到keychain里面灵奖,需要用到的時候從keychain中取嚼沿,這樣可以保證UUID的唯一性。
首先瓷患,我們新建一個類AppKeyChain
骡尽,管理keychain.
在AppKeyChain.h
中
@interface AppKeyChain : NSObject
+ (void)saveData:(id)data forKey:(NSString *)key;//將數(shù)據(jù)寫入keychain中
+ (id)loadForKey:(NSString *)key;//從keychain中獲取數(shù)據(jù)
+ (void)deleteKeyData:(NSString *)key;//刪除keychain中的相應(yīng)數(shù)據(jù)
@end
AppKeyChain.m
中實現(xiàn)相應(yīng)的方法
#import "AppKeyChain.h"
#import <Security/Security.h>
@implementation AppKeyChain
#pragma mark - 保存和讀取UUID
+ (NSMutableDictionary *)getKeychainQuery:(NSString *)key {
return [NSMutableDictionary dictionaryWithObjectsAndKeys:
(id)kSecClassGenericPassword,(id)kSecClass,
key, (id)kSecAttrService,
key, (id)kSecAttrAccount,
(id)kSecAttrAccessibleAfterFirstUnlock,(id)kSecAttrAccessible, nil];
}
+ (void)saveData:(id)data forKey:(NSString *)key {
//獲得keychain字典
NSMutableDictionary *keychainQuery = [self getKeychainQuery:key];
//刪除舊值
SecItemDelete((CFDictionaryRef)keychainQuery);
//添加新值
[keychainQuery setObject:[NSKeyedArchiver archivedDataWithRootObject:data] forKey:(id)kSecValueData];
// 將新的信息添加到keychain中
SecItemAdd((CFDictionaryRef)keychainQuery, NULL);
}
+ (id)loadForKey:(NSString *)key {
id ret = nil;
NSMutableDictionary *keychainQuery = [self getKeychainQuery:key];
//Configure the search setting
//Since in our simple case we are expecting only a single attribute to be returned (the password) we can set the attribute kSecReturnData to kCFBooleanTrue
[keychainQuery setObject:(id)kCFBooleanTrue forKey:(id)kSecReturnData];
[keychainQuery setObject:(id)kSecMatchLimitOne forKey:(id)kSecMatchLimit];
CFDataRef keyData = NULL;
if (SecItemCopyMatching((CFDictionaryRef)keychainQuery, (CFTypeRef *)&keyData) == noErr) {
@try {
ret = [NSKeyedUnarchiver unarchiveObjectWithData:(__bridge NSData *)keyData];
} @catch (NSException *e) {
NSLog(@"存儲失敗,key-- %@ exception-- %@", key, e);
} @finally {
}
}
if (keyData) {
CFRelease(keyData);
}
return ret;
}
+ (void)deleteKeyData:(NSString *)key {
NSMutableDictionary *keychainQuery = [self getKeychainQuery:key];
SecItemDelete((CFDictionaryRef)keychainQuery);
}
@end
然后擅编,再創(chuàng)建一個管理UUID的類UUIDManager
在` UUIDManager.h`中聲明一個獲取設(shè)備UUID的方法
#import <Foundation/Foundation.h>
@interface UUIDManager : NSObject
// 獲取設(shè)備唯一標(biāo)識
+ (NSString *)getDeviceID;
@end
在UUIDManager.m
中實現(xiàn)對應(yīng)的方法
#import "UUIDManager.h"
#import "AppKeyChain.h"
@implementation UUIDManager
+ (NSString *)getDeviceID {
// 讀取keyChain存儲的UUID
NSString * strUUID = (NSString *)[AppKeyChain loadForKey: @"uuid"];
// 首次運(yùn)行生成一個UUID并用keyChain存儲
if ([strUUID isEqualToString: @""] || !strUUID) {
// 生成uuid
CFUUIDRef uuidRef = CFUUIDCreate(kCFAllocatorDefault);
strUUID = (NSString *)CFBridgingRelease(CFUUIDCreateString (kCFAllocatorDefault,uuidRef));
// 將該uuid用keychain存儲
[AppKeyChain saveData: strUUID forKey: @"uuid"];
}
return strUUID;
}
@end
最后攀细,將UUIDManager
導(dǎo)入到自己的工程中。
在AppDelegate.m
將UUID存儲下來
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//存儲設(shè)備的UUID
[UUIDManager getDeviceID];
return YES;
}
這樣爱态,設(shè)備的UUID已經(jīng)被存儲到keychain中谭贪,這時候你就可以隨時隨地獲取設(shè)備的唯一標(biāo)識UUID了
比如我在ViewController.m
中取用UUID,直接進(jìn)入UUIDManager
- (void)viewDidLoad {
[super viewDidLoad];
NSString *deviceUUID = [UUIDManager getDeviceID];
NSLog(@"獲取設(shè)備的UUID:%@",deviceUUID);
}