原文 http://www.cnblogs.com/yssgyw/p/3364370.html
本文是iOS7系列文章第一篇文章,主要介紹使用KeyChain儲存和擷取APP資料,解決iOS7上擷取不變UDID的問題。并給出一個擷取UDID的工具類,使用友善,隻需要替換兩個地方即可。
一、iOS不用版本擷取UDID的方法比較
1)iOS 5.0
iOS 2.0版本以後UIDevice提供一個擷取裝置唯一辨別符的方法uniqueIdentifier,通過該方法我們可以擷取裝置的序列号,這個也是目前 為止唯一可以确認唯一的标示符。好景不長,因為該唯一辨別符與手機一一對應,蘋果覺得可能會洩露使用者隐私,是以在 iOS 5.0之後該方法就被廢棄掉了。
而且蘋果做的更狠,今年5月份以後送出App Store的産品都不允許再用uniqueIdentifier接口,甚至有些朋友因為代碼中有UDID還被打回來,看來這條路是被封死了。
2)iOS 6.0
iOS 6.0系統新增了兩個用于替換uniqueIdentifier的接口,分别是:identifierForVendor,advertisingIdentifier。
identifierForVendor 接口的官方文檔介紹如下:

The value of this property is the same for apps that come from the same vendor running on the same device. A different value is returned for apps on the same device that come from different vendors, and for apps on different devices regardless of vendor.
The value of this property may be nil if the app is running in the background, before the user has unlocked the device the first time after the device has been restarted. If the value is nil, wait and get the value again later.
The value in this property remains the same while the app (or another app from the same vendor) is installed on the iOS device. The value changes when the user deletes all of that vendor’s apps from the device and subsequently reinstalls one or more of them. Therefore, if your app stores the value of this property anywhere, you should gracefully handle situations where the identifier changes.
大概意思就是“同一開發商的APP在指定機器上都會獲得同一個ID。當我們删除了某一個裝置上某個開發商的所有APP之後,下次擷取将會擷取到不同的 ID。” 也就是說我們通過該接口不能擷取用來唯一辨別裝置的ID,問題總是難不倒聰明的程式員,于是大家想到了使用WiFi的mac位址來取代已經廢棄了的 uniqueIdentifier方法。具體的方法晚上有很多,大家感興趣的可以自己找找,這兒提供一個網址: http://stackoverflow.com/questions/677530/how-can-i-programmatically-get-the-mac-address-of-an-iphone
3)iOS 7.0
iOS 7中蘋果再一次無情的封殺mac位址,使用之前的方法擷取到的mac位址全部都變成了02:00:00:00:00:00。有問題總的解決啊,于是四處查 資料,終于有了思路是否可以使用KeyChain來儲存擷取到的唯一标示符呢,這樣以後即使APP删了再裝回來,也可以從KeyChain中讀取回來。有 了方向以後就開始做,看關于KeyChain的官方文檔,看官方使用KeyChain的Demo,大概花了一下午時間,問題終于解決了。
二、KeyChain介紹
我們搞iOS開發,一定都知道OS X裡面的KeyChain(鑰匙串),通常要鄉鎮及調試的話,都得安裝證書之類的,這些證書就是儲存在KeyChain中,還有我們平時浏覽網頁記錄的賬 号密碼也都是記錄在KeyChain中。iOS中的KeyChain相比OS X比較簡單,整個系統隻有一個KeyChain,每個程式都可以往KeyChain中記錄資料,而且隻能讀取到自己程式記錄在KeyChain中的資料。 iOS中Security.framework架構提供了四個主要的方法來操作KeyChain:
// 查詢
OSStatus SecItemCopyMatching(CFDictionaryRef query, CFTypeRef *result);
// 添加
OSStatus SecItemAdd(CFDictionaryRef attributes, CFTypeRef *result);
// 更新KeyChain中的Item
OSStatus SecItemUpdate(CFDictionaryRef query, CFDictionaryRef attributesToUpdate);
// 删除KeyChain中的Item
OSStatus SecItemDelete(CFDictionaryRef query)

這四個方法參數比較複雜,一旦傳錯就會導緻操作KeyChain失敗,這塊兒文檔中介紹的比較詳細,大家可以查查官方文檔 Keychain Services Reference 。
前面提到了每個APP隻允許通路自己在KeyChain中記錄的資料,那麼是不是就沒有别的辦法通路其他APP存在KeyChain的資料了?
蘋果提供了一個方法允許同一個發商的多個APP通路各APP之間的途徑,即在調SecItemAdd添加資料的時候指定AccessGroup,即通路組。一個APP可以屬于同僚屬于多個分組,添加KeyChain資料通路組需要做一下兩件事情:
a、在APP target的bulibSetting裡面設定Code Signing Entitlements,指向包含AceessGroup的分組資訊的plist檔案。該檔案必須和工程檔案在同一個目錄下,我在添加通路分組的時候就 因為plist檔案位置問題,操作KeyChain失敗,查找這個問題還花了好久的時間。
b、在工程目錄下建立一個KeychainAccessGroups.plist檔案,該檔案的結構中最頂層的節點必須是一個名為“keychain-access-groups”的Array,并且該Array中每一項都是一個描述分組的NSString。 對于String的格式也有相應要求,格式為:"AppIdentifier.com.***",其中APPIdentifier就是你的開發者帳号對應的ID。
c、在代碼中往KeyChain中Add資料的時候,設定kSecAttrAccessGroup,代碼如下:
NSString *accessGroup = [NSString stringWithUTF8String:"APPIdentifier.com.cnblogs.smileEvday"];
if (accessGroup != nil)
{
#if TARGET_IPHONE_SIMULATOR
// Ignore the access group if running on the iPhone simulator.
//
// Apps that are built for the simulator aren't signed, so there's no keychain access group
// for the simulator to check. This means that all apps can see all keychain items when run
// on the simulator.
//
// If a SecItem contains an access group attribute, SecItemAdd and SecItemUpdate on the
// simulator will return -25243 (errSecNoAccessForItem).
#else
[dictForQuery setObject:accessGroup forKey:(id)kSecAttrAccessGroup];
#endif
}

這段代碼是從官方的Demo中直接拷貝過來的,根據注釋我們可以看到,模拟器是不支援AccessGroup的,是以才行了預編譯宏來選擇性添加。
注:appIdentifer就是開發者帳号的那一串辨別,如下圖所示:
打開xcode的Organizer,選擇Device頁籤,連接配接裝置就可以看到裝置上安裝的開發者賬号描述檔案清單,其中第五列最開始的10個字元即為App Identifier,這塊兒前面寫的不是很清楚,好多朋友加我qq問我,今天特地補上。
三、使用KeyChain儲存和擷取UDID
說了這麼多終于進入正題了,如何在iOS 7上面擷取到不變的UDID。我們将第二部分所講的知識直接應用進來就可以了輕松達到我們要的效果了,下面我們先看看往如何将擷取到的identifierForVendor添加到KeyChain中的代碼。
+ (BOOL)settUDIDToKeyChain:(NSString*)udid
{
NSMutableDictionary *dictForAdd = [[NSMutableDictionary alloc] init];
[dictForAdd setValue:(id)kSecClassGenericPassword forKey:(id)kSecClass];
[dictForAdd setValue:[NSString stringWithUTF8String:kKeychainUDIDItemIdentifier] forKey:kSecAttrDescription];
[dictForAdd setValue:@"UUID" forKey:(id)kSecAttrGeneric];
// Default attributes for keychain item.
[dictForAdd setObject:@"" forKey:(id)kSecAttrAccount];
[dictForAdd setObject:@"" forKey:(id)kSecAttrLabel];
// The keychain access group attribute determines if this item can be shared// amongst multiple apps whose code signing entitlements contain the same keychain access group.
NSString *accessGroup = [NSString stringWithUTF8String:kKeyChainUDIDAccessGroup];
if (accessGroup != nil)
{
#if TARGET_IPHONE_SIMULATOR
// Ignore the access group if running on the iPhone simulator.//
// Apps that are built for the simulator aren't signed, so there's no keychain access group// for the simulator to check. This means that all apps can see all keychain items when run// on the simulator.//
// If a SecItem contains an access group attribute, SecItemAdd and SecItemUpdate on the// simulator will return -25243 (errSecNoAccessForItem).
#else
[dictForAdd setObject:accessGroup forKey:(id)kSecAttrAccessGroup];
#endif
}
const char *udidStr = [udid UTF8String];
NSData *keyChainItemValue = [NSData dataWithBytes:udidStr length:strlen(udidStr)];
[dictForAdd setValue:keyChainItemValue forKey:(id)kSecValueData];
OSStatus writeErr = noErr;
if ([SvUDIDTools getUDIDFromKeyChain]) { // there is item in keychain
[SvUDIDTools updateUDIDInKeyChain:udid];
[dictForAdd release];
return YES;
}
else { // add item to keychain
writeErr = SecItemAdd((CFDictionaryRef)dictForAdd, NULL);
if (writeErr != errSecSuccess) {
NSLog(@"Add KeyChain Item Error!!! Error Code:%ld", writeErr);
[dictForAdd release];
return NO;
}
else {
NSLog(@"Add KeyChain Item Success!!!");
[dictForAdd release];
return YES;
}
}
[dictForAdd release];
return NO;
}

上面代碼中,首先建構一個要添加到KeyChain中資料的Dictionary,包含一些基本的KeyChain Item的資料類型,描述,通路分組以及最重要的資料等資訊,最後通過調用SecItemAdd方法将我們需要儲存的UUID儲存到KeyChain中。
擷取KeyChain中相應資料的代碼如下:
+ (NSString*)getUDIDFromKeyChain
{
NSMutableDictionary *dictForQuery = [[NSMutableDictionary alloc] init];
[dictForQuery setValue:(id)kSecClassGenericPassword forKey:(id)kSecClass];
// set Attr Description for query
[dictForQuery setValue:[NSString stringWithUTF8String:kKeychainUDIDItemIdentifier]
forKey:kSecAttrDescription];
// set Attr Identity for query
NSData *keychainItemID = [NSData dataWithBytes:kKeychainUDIDItemIdentifier
length:strlen(kKeychainUDIDItemIdentifier)];
[dictForQuery setObject:keychainItemID forKey:(id)kSecAttrGeneric];
// The keychain access group attribute determines if this item can be shared// amongst multiple apps whose code signing entitlements contain the same keychain access group.
NSString *accessGroup = [NSString stringWithUTF8String:kKeyChainUDIDAccessGroup];
if (accessGroup != nil)
{
#if TARGET_IPHONE_SIMULATOR
// Ignore the access group if running on the iPhone simulator.//
// Apps that are built for the simulator aren't signed, so there's no keychain access group// for the simulator to check. This means that all apps can see all keychain items when run// on the simulator.//
// If a SecItem contains an access group attribute, SecItemAdd and SecItemUpdate on the// simulator will return -25243 (errSecNoAccessForItem).
#else
[dictForQuery setObject:accessGroup forKey:(id)kSecAttrAccessGroup];
#endif
}
[dictForQuery setValue:(id)kCFBooleanTrue forKey:(id)kSecMatchCaseInsensitive];
[dictForQuery setValue:(id)kSecMatchLimitOne forKey:(id)kSecMatchLimit];
[dictForQuery setValue:(id)kCFBooleanTrue forKey:(id)kSecReturnData];
OSStatus queryErr = noErr;
NSData *udidValue = nil;
NSString *udid = nil;
queryErr = SecItemCopyMatching((CFDictionaryRef)dictForQuery, (CFTypeRef*)&udidValue);
NSMutableDictionary *dict = nil;
[dictForQuery setValue:(id)kCFBooleanTrue forKey:(id)kSecReturnAttributes];
queryErr = SecItemCopyMatching((CFDictionaryRef)dictForQuery, (CFTypeRef*)&dict);
if (queryErr == errSecItemNotFound) {
NSLog(@"KeyChain Item: %@ not found!!!", [NSString stringWithUTF8String:kKeychainUDIDItemIdentifier]);
}
else if (queryErr != errSecSuccess) {
NSLog(@"KeyChain Item query Error!!! Error code:%ld", queryErr);
}
if (queryErr == errSecSuccess) {
NSLog(@"KeyChain Item: %@", udidValue);
if (udidValue) {
udid = [NSString stringWithUTF8String:udidValue.bytes];
}
}
[dictForQuery release];
return udid;
}
上面代碼的流程也差不多一樣,首先建立一個Dictionary,其中設定一下查找條件,然後通過SecItemCopyMatching方法擷取到我們之前儲存到KeyChain中的資料。
四、總結
本文介紹了使用KeyChain實作APP删除後依然可以擷取到相同的UDID資訊的解決方法。
你可能有疑問,如果系統更新以後,是否仍然可以擷取到之前記錄的UDID資料?
答案是肯定的,這一點我專門做了測試。就算我們程式删除掉,系統經過更新以後再安裝回來,依舊可以擷取到與之前一緻的UDID。但是當我們把整 個系統還原以後是否還能擷取到之前記錄的UDID,這一點我覺得應該不行,不過手機裡面資料太多,沒有測試,如果大家有興趣可以測試一下,驗證一下我的猜 想。
完整代碼位址: https://github.com/smileEvday/SvUDID
大家如果要在真機運作時,需要替換兩個地方:
第一個地方是plist檔案中的accessGroup中的APPIdentifier。
第二個地方是SvUDIDTools.m中的kKeyChainUDIDAccessGroup的APPIdentity為你所使用的profile的APPIdentifier。
文章和代碼中如果有什麼不對的地方,歡迎指正,在這兒先謝過了。
注:如果覺得本文幫到了你,别忘了點推薦
轉載請著名出處,有什麼問題歡迎留言
也可以加本人QQ: 1592232964,歡迎一起讨論iOS開發的知識!!!
轉載于:https://www.cnblogs.com/lisa090818/p/3429675.html