2015-01-02 65 views
0

我在iOS開發的新手,我想存儲在鑰匙串我的應用程序UUID所以任何時候我申請UUID其餘同我做就可以了 - [R & d和查找本網站我亂StackOver流程的代碼是一樣的代碼如何保存UUID在鑰匙串的iOS?

+(NSUUID *)persistentIdentifierForVendor 
{ 
static NSString * const kKeyChainVendorID = @"co.cwbrn.PersistentIdentifier"; 
static NSString * const kKeyChainVendorIDAccessGroup = @"<AppIdentifier>.<keychain-access-group-identifier>"; 

// First, check NSUserDefaults so that we're not hitting the KeyChain every single time 
NSString *uuidString = [[NSUserDefaults standardUserDefaults] stringForKey:kKeyChainVendorIDGroup]; 
BOOL vendorIDMissingFromUserDefaults = (uuidString == nil || uuidString.length == 0); 

if (vendorIDMissingFromUserDefaults) { 
    // Check to see if a UUID is stored in the KeyChain 
    NSDictionary *query = @{ 
          (__bridge id)kSecClass:    (__bridge id)kSecClassGenericPassword, 
          (__bridge id)kSecAttrAccount:  kKeyChainVendorID, 
          (__bridge id)kSecAttrService:  kKeyChainVendorID, 
          (__bridge id)kSecAttrAccessGroup: kKeyChainVendorIDAccessGroup, 
          (__bridge id)kSecMatchLimit:  (__bridge id)kSecMatchLimitOne, 
          (__bridge id)kSecReturnAttributes: (__bridge id)kCFBooleanTrue 
          }; 
    CFTypeRef attributesRef = NULL; 
    OSStatus result = SecItemCopyMatching((__bridge CFDictionaryRef)query, &attributesRef); 
    if (result == noErr) { 
     // There is a UUID, so try to retrieve it 
     NSDictionary *attributes = (__bridge_transfer NSDictionary *)attributesRef; 
     NSMutableDictionary *valueQuery = [NSMutableDictionary dictionaryWithDictionary:attributes]; 

     [valueQuery setObject:(__bridge id)kSecClassGenericPassword forKey:(__bridge id)kSecClass]; 
     [valueQuery setObject:(__bridge id)kCFBooleanTrue   forKey:(__bridge id)kSecReturnData]; 

     CFTypeRef passwordDataRef = NULL; 
     OSStatus result = SecItemCopyMatching((__bridge CFDictionaryRef)valueQuery, &passwordDataRef); 
     if (result == noErr) { 
      NSData *passwordData = (__bridge_transfer NSData *)passwordDataRef; 
      uuidString = [[NSString alloc] initWithBytes:[passwordData bytes] 
                length:[passwordData length] 
               encoding:NSUTF8StringEncoding]; 
     } 
    } 
} 

// Failed to read the UUID from the KeyChain, so create a new UUID and store it 
if (uuidString == nil || uuidString.length == 0) { 
    // Generate the new UIID 
    CFUUIDRef uuidRef = CFUUIDCreate(kCFAllocatorDefault); 
    uuidString = (__bridge_transfer NSString *)CFUUIDCreateString(kCFAllocatorDefault, uuidRef); 
    CFRelease(uuidRef); 

    // Now store it in the KeyChain 
    NSDictionary *query = @{ (__bridge id)kSecClass:    (__bridge id)kSecClassGenericPassword, 
           (__bridge id)kSecAttrAccount:  kKeyChainVendorID, 
           (__bridge id)kSecAttrService:  kKeyChainVendorID, 
           (__bridge id)kSecAttrAccessGroup: kKeyChainVendorIDAccessGroup, 
           (__bridge id)kSecAttrLabel:   @"", 
           (__bridge id)kSecAttrDescription: @"", 
           (__bridge id)kSecAttrAccessible: (__bridge id)kSecAttrAccessibleAfterFirstUnlock, 
           (__bridge id)kSecValueData:   [uuidString dataUsingEncoding:NSUTF8StringEncoding] 
          }; 

    OSStatus result = SecItemAdd((__bridge CFDictionaryRef)query, NULL); 
    if (result != noErr) { 
     NSLog(@"ERROR: Couldn't add to the Keychain. Result = %ld; Query = %@", result, query); 
     return nil; 
    } 
} 

// Save UUID to NSUserDefaults so that we can avoid the KeyChain next time 
if (vendorIDMissingFromUserDefaults) { 
    [[NSUserDefaults standardUserDefaults] setObject:uuidString forKey:kKeyChainVendorIDGroup]; 
} 

return [[NSUUID alloc] initWithUUIDString:uuidString]; 

}

,但我想知道這裏是什麼kKeyChainVendorIDkKeyChainVendorIDAccessGroup這裏使用的是像爲

static NSString * const kKeyChainVendorID = @"co.cwbrn.PersistentIdentifier"; 
static NSString * const kKeyChainVendorIDAccessGroup = @"<AppIdentifier>.<keychain-access-group-identifier>"; 

對於我的AP摺疊我如何得到兩個值等作爲kKeyChainVendorIDkKeyChainVendorIDAccessGroup?請給我解決方案,用於在我的Xcode 5.0版本的錯誤都發生在行

NSString *uuidString = [[NSUserDefaults standardUserDefaults] stringForKey:kKeyChainVendorIDGroup]; 

ERRO是: - 與kKeyChainVendorID更換kKeyChainVendorIDGroup。我可以取代它則是工作還是不請給我解決方案對於我的問題都提前

感謝。並感謝nelico發佈堆棧溢出回答。

+0

@AbhishekSharma我把那個鏈接的代碼,但我想知道什麼是'kKeyChainVendorID'和'kKeyChainVendorIDAccessGroup',我如何得到它我的應用程序,如果你知道那麼請給我解決方案和閱讀問題充分關懷。 –

+0

我想你可以給你的應用程序提供任何名稱。但是如果你保持命名慣例,那麼它會更好。我使用KeyChain作爲Access組。 – Janmenjaya

回答

2

這裏我發表我自己的答案。我得到的答案從這個鏈接

http://objectivecwithsuraj.blogspot.in/2014/01/unique-identifier-uuid-ios.html 

我使用FDKeyChain和寫入下面的代碼保存UUID在鑰匙串

只是定義了兩個字符串等作爲

static NSString * const KeychainItem_Service = @"FDKeychain"; 
static NSString * const KeychainItem_UUID = @"Local"; 

,併爲獲取UUID我寫的

uniqueIdentifier=[self generateUUID]; 
-(NSString *)generateUUID { 
NSString *CFUUID = nil; 

if (![FDKeychain itemForKey: KeychainItem_UUID 
       forService: KeychainItem_Service 
         error: nil]) { 
    CFUUIDRef uuid = CFUUIDCreate(kCFAllocatorDefault); 

    CFUUID = (NSString *)CFBridgingRelease(CFUUIDCreateString(kCFAllocatorDefault, uuid)); 

    [FDKeychain saveItem: CFUUID 
        forKey: KeychainItem_UUID 
       forService: KeychainItem_Service 
        error: nil]; 

} else { 
    CFUUID = [FDKeychain itemForKey: KeychainItem_UUID 
         forService: KeychainItem_Service 
           error: nil]; 
} 

return CFUUID; 

}

希望它可以幫助的人。如果我在我的回答中出現問題,請給我解決方案。感謝您的回覆。

相關問題