ios keychain 介紹

Download iOS Keychain 介紹

Post on 14-Jul-2015




3 download

Embed Size (px)


iOS KeyChain

iOS KeyChainHappyMan2014/12/30

What is KeyChainKeychain is an encrypted container where you can store secured information like passwords, certificates, identities, etc.In iOS, each application has its own keychain.To share the data between apps, they should have the same Access Group in code signing entitlements.Accessing password-protected services using a keychain in OS X

Accessing an Internet server using iPhone Keychain Services

KeyChain iOS keychain /private/var/Keychains/keychain-2.db( keychain-access-groups ) app app keychain app app keychain app keychain keychain

APISecItemAddSecItemCopyMatchingSecItemUpdateSecItemDelete-(NSMutableDictionary *) prepareDict:(NSString *)key{ NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; [dict setObject:(__bridge id)kSecClassGenericPassword forKey:(__bridge id)kSecClass]; NSData *encodedKey = [key dataUsingEncoding:NSUTF8StringEncoding]; [dict setObject:encodedKey forKey:(__bridge id)kSecAttrGeneric]; [dict setObject:encodedKey forKey:(__bridge id)kSecAttrAccount]; [dict setObject:service forKey:(__bridge id)kSecAttrService]; [dict setObject:(__bridge id)kSecAttrAccessibleAlwaysThisDeviceOnly forKey:(__bridge id)kSecAttrAccessible]; return dict;}-(BOOL) insert:(NSString *)key :(NSData *)data{ NSMutableDictionary *dict =[self prepareDict:key]; [dict setObject:data forKey:(__bridge id)kSecValueData]; OSStatus status = SecItemAdd((__bridge CFDictionaryRef)dict, NULL); if(errSecSuccess != status) { NSLog(@"Unable add item with key = %@ error: %d",key,(int)status); } return (status == errSecSuccess);}-(NSData*) find:(NSString *)key{ NSMutableDictionary *dict = [self prepareDict:key]; [dict setObject:(__bridge id)kSecMatchLimitOne forKey:(__bridge id)kSecMatchLimit]; [dict setObject:(id)kCFBooleanTrue forKey:(__bridge id)kSecReturnData]; CFTypeRef result = NULL; OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef)dict,&result); if(status != errSecSuccess) { NSLog(@"Unable to fetch item for key %@ with error: %d",key,(int)status); return nil; } return (__bridge NSData *)result;}-(BOOL) update:(NSString*)key :(NSData *)data{ NSMutableDictionary *dictKey =[self prepareDict:key]; NSMutableDictionary *dictUpdate =[[NSMutableDictionary alloc] init]; [dictUpdate setObject:data forKey:(__bridge id)kSecValueData]; OSStatus status = SecItemUpdate((__bridge CFDictionaryRef)dictKey, (__bridge CFDictionaryRef)dictUpdate); if(status != errSecSuccess) { NSLog(@"Unable add update with key = %@ error: %d",key,(int)status); } return (status == errSecSuccess);}-(BOOL) remove:(NSString *)key{ NSMutableDictionary *dict = [self prepareDict:key]; OSStatus status = SecItemDelete((__bridge CFDictionaryRef)dict); if(status != errSecSuccess) { NSLog(@"Unable to remove item for key %@ with error: %d",key,(int)status); } return (status == errSecSuccess);}SSKeychain 1730 (2014/12/30)SFHFKeychainUtils 60 (2014/12/30)Me: 2 projects

Demo Initialization of the class2). How to Add an item to keychain3). Find an item in the keychain4). Update an item in the keychain5). Remove an item from keychain

iOS KeyChain Tutorial and Encrypting Data on iOS Security in iOS 5 Part 1 Security in iOS 5 Part 2

iOS Keychain: Sharing data between apps Group Access KeyChain

AppleKeychain Services Programming Guide Services Reference session 711 - Keychain and Authentication with Touch ID