iOS 鑰匙串儲存使用者資料

來源:互聯網
上載者:User

標籤:led   string   exce   使用者數   div   interface   nslog   lock   pre   

參考: http://www.jianshu.com/p/f6d40065bb6c

 

#import <Foundation/Foundation.h>#import <Security/Security.h>@interface JCKeychainTool : NSObject/** *  儲存字串到??鑰匙串 * *  @param sValue 對應的Value *  @param sKey   對應的Key */+ (void)saveKeychainValue:(NSString *)sValue key:(NSString *)sKey;/** *  從??鑰匙串擷取字串 * *  @param sKey 對應的Key * *  @return 返回儲存的Value */+ (NSString *)readKeychainValue:(NSString *)sKey;/** *  從??鑰匙串刪除字串 * *  @param sKey 對應的Key */+ (void)deleteKeychainValue:(NSString *)sKey;@end

 

#import "JCKeychainTool.h"@implementation JCKeychainTool+ (NSMutableDictionary *)getKeychainQuery:(NSString *)service{    return [NSMutableDictionary dictionaryWithObjectsAndKeys:            (__bridge_transfer id)kSecClassGenericPassword,            (__bridge_transfer id)kSecClass,service,            (__bridge_transfer id)kSecAttrService,service,            (__bridge_transfer id)kSecAttrAccount,            (__bridge_transfer id)kSecAttrAccessibleAfterFirstUnlock,            (__bridge_transfer id)kSecAttrAccessible,            nil];}+ (void)saveKeychainValue:(NSString *)sValue key:(NSString *)sKey{    NSMutableDictionary * keychainQuery = [self getKeychainQuery:sKey];    SecItemDelete((__bridge_retained CFDictionaryRef)keychainQuery);        [keychainQuery setObject:[NSKeyedArchiver archivedDataWithRootObject:sValue] forKey:(__bridge_transfer id)kSecValueData];        SecItemAdd((__bridge_retained CFDictionaryRef)keychainQuery, NULL);    }+ (NSString *)readKeychainValue:(NSString *)sKey{    NSString *ret = nil;    NSMutableDictionary *keychainQuery = [self getKeychainQuery:sKey];    [keychainQuery setObject:(id)kCFBooleanTrue forKey:(__bridge_transfer id)kSecReturnData];    [keychainQuery setObject:(__bridge_transfer id)kSecMatchLimitOne forKey:(__bridge_transfer id)kSecMatchLimit];    CFDataRef keyData = NULL;    if (SecItemCopyMatching((__bridge CFDictionaryRef)keychainQuery, (CFTypeRef *)&keyData) == noErr) {        @try {            ret = (NSString *)[NSKeyedUnarchiver unarchiveObjectWithData:(__bridge NSData *)keyData];        } @catch (NSException *e) {            NSLog(@"Unarchive of %@ failed: %@", sKey, e);        } @finally {        }    }    if (keyData)        CFRelease(keyData);    return ret;}+ (void)deleteKeychainValue:(NSString *)sKey {    NSMutableDictionary *keychainQuery = [self getKeychainQuery:sKey];    SecItemDelete((__bridge CFDictionaryRef)keychainQuery);}@end

 

 // 存[JCKeychainTool saveKeychainValue:@"xxx" key:@"userName"];[JCKeychainTool saveKeychainValue:@"yyy"key:@"password"];// 讀[JCKeychainTool readKeychainValue:@"userName"];[JCKeychainTool readKeychainValue:@"password"];// 刪[JCKeychainTool deleteKeychainValue:@"userName"];[JCKeychainTool deleteKeychainValue:@"password"];

iOS 鑰匙串儲存使用者資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.