在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):kishikawakatsumi/UICKeyChainStore开源软件地址(OpenSource Url):https://github.com/kishikawakatsumi/UICKeyChainStore开源编程语言(OpenSource Language):Objective-C 91.6%开源软件介绍(OpenSource Introduction):UICKeyChainStoreUICKeyChainStore is a simple wrapper for Keychain that works on iOS and OS X. Makes using Keychain APIs as easy as NSUserDefaults. Looking for the library written in Swift?Try KeychainAccess. Transitioning from 1.x to 2.0
Features
UsageBasicsSaving Application PasswordUICKeyChainStore *keychain = [UICKeyChainStore keyChainStoreWithService:@"com.example.github-token"];
keychain[@"kishikawakatsumi"] = @"01234567-89ab-cdef-0123-456789abcdef"; Saving Internet PasswordUICKeyChainStore *keychain = [UICKeyChainStore keyChainStoreWithServer:[NSURL URLWithString:@"https://github.com"]
protocolType:UICKeyChainStoreProtocolTypeHTTPS];
keychain[@"kishikawakatsumi"] = @"01234567-89ab-cdef-0123-456789abcdef"; InstantiationCreate Keychain for Application PasswordUICKeyChainStore *keychain = [UICKeyChainStore keyChainStoreWithService:@"com.example.github-token"]; UICKeyChainStore *keychain = [UICKeyChainStore keyChainStoreWithService:@"kishikawakatsumi.git"
accessGroup:@"12ABCD3E4F.shared"]; Create Keychain for Internet PasswordUICKeyChainStore *keychain = [UICKeyChainStore keyChainStoreWithServer:[NSURL URLWithString:@"https://github.com"]
protocolType:UICKeyChainStoreProtocolTypeHTTPS]; UICKeyChainStore *keychain = [UICKeyChainStore keyChainStoreWithServer:[NSURL URLWithString:@"https://github.com"]
protocolType:UICKeyChainStoreProtocolTypeHTTPS
authenticationType:UICKeyChainStoreAuthenticationTypeHTMLForm]; Adding an itemsubscriptingkeychain[@"kishikawakatsumi"] = @"01234567-89ab-cdef-0123-456789abcdef" set method[keychain setString:@"01234567-89ab-cdef-0123-456789abcdef" forKey:@"kishikawakatsumi"]; error handlingif (![keychain setString:@"01234567-89ab-cdef-0123-456789abcdef" forKey:@"kishikawakatsumi"]) {
// error has occurred
} NSError *error;
[keychain setString:@"01234567-89ab-cdef-0123-456789abcdef" forKey:@"kishikawakatsumi" error:&error];
if (error) {
NSLog(@"%@", error.localizedDescription);
} Obtaining an itemsubscripting (automatically converts to string)NSString *token = keychain[@"kishikawakatsumi"] get methodsas StringNSString *token = [keychain stringForKey:@"kishikawakatsumi"]; as NSDataNSData *data = [keychain dataForKey:@"kishikawakatsumi"]; error handlingFirst, get the NSError *error;
NSString *token = [keychain stringForKey:@"" error:&error];
if (error) {
NSLog(@"%@", error.localizedDescription);
} Removing an itemsubscriptingkeychain[@"kishikawakatsumi"] = nil remove method[keychain removeItemForKey:@"kishikawakatsumi"]; error handlingif (![keychain removeItemForKey:@"kishikawakatsumi"]) {
// error has occurred
} NSError *error;
[keychain removeItemForKey:@"kishikawakatsumi" error:&error];
if (error) {
NSLog(@"%@", error.localizedDescription);
} Label and CommentUICKeyChainStore *keychain = [UICKeyChainStore keyChainStoreWithServer:[NSURL URLWithString:@"https://github.com"]
protocolType:UICKeyChainStoreProtocolTypeHTTPS];
[keychain setString:@"01234567-89ab-cdef-0123-456789abcdef"
forKey:@"kishikawakatsumi"
label:@"github.com (kishikawakatsumi)"
comment:@"github access token"]; Configuration (Accessibility, Sharing, iCould Sync)AccessibilityDefault accessibility matches background application (=kSecAttrAccessibleAfterFirstUnlock)UICKeyChainStore *keychain = [UICKeyChainStore keyChainStoreWithService:@"com.example.github-token"]; For background applicationCreating instanceUICKeyChainStore *keychain = [UICKeyChainStore keyChainStoreWithService:@"com.example.github-token"];
keychain.accessibility = UICKeyChainStoreAccessibilityAfterFirstUnlock;
keychain[@"kishikawakatsumi"] = @"01234567-89ab-cdef-0123-456789abcdef" For foreground applicationCreating instanceUICKeyChainStore *keychain = [UICKeyChainStore keyChainStoreWithService:@"com.example.github-token"];
keychain.accessibility = UICKeyChainStoreAccessibilityWhenUnlocked;
keychain[@"kishikawakatsumi"] = @"01234567-89ab-cdef-0123-456789abcdef" Sharing Keychain itemsUICKeyChainStore *keychain = [UICKeyChainStore keyChainStoreWithService:@"kishikawakatsumi.git"
accessGroup:@"12ABCD3E4F.shared"]; Synchronizing Keychain items with iCloudCreating instanceUICKeyChainStore *keychain = [UICKeyChainStore keyChainStoreWithService:@"com.example.github-token"];
keychain.synchronizable = YES;
keychain[@"kishikawakatsumi"] = @"01234567-89ab-cdef-0123-456789abcdef" Touch ID integrationAny Operation that require authentication must be run in the background thread. Adding a Touch ID protected itemIf you want to store the Touch ID protected Keychain item, specify UICKeyChainStore *keychain = [UICKeyChainStore keyChainStoreWithService:@"com.example.github-token"];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
[keychain setAccessibility:UICKeyChainStoreAccessibilityWhenPasscodeSetThisDeviceOnly
authenticationPolicy:UICKeyChainStoreAuthenticationPolicyUserPresence];
keychain[@"kishikawakatsumi"] = @"01234567-89ab-cdef-0123-456789abcdef"
}); Updating a Touch ID protected itemThe same way as when adding. Do not run in the main thread if there is a possibility that the item you are trying to add already exists, and protected. Because updating protected items requires authentication. Additionally, you want to show custom authentication prompt message when updating, specify an UICKeyChainStore *keychain = [UICKeyChainStore keyChainStoreWithService:@"com.example.github-token"];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
[keychain setAccessibility:UICKeyChainStoreAccessibilityWhenPasscodeSetThisDeviceOnly
authenticationPolicy:UICKeyChainStoreAuthenticationPolicyUserPresence];
keychain.authenticationPrompt = @"Authenticate to update your access token";
keychain[@"kishikawakatsumi"] = @"01234567-89ab-cdef-0123-456789abcdef"
}); Obtaining a Touch ID protected itemThe same way as when you get a normal item. It will be displayed automatically Touch ID or passcode authentication If the item you try to get is protected. UICKeyChainStore *keychain = [UICKeyChainStore keyChainStoreWithService:@"com.example.github-token"];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
[keychain setAccessibility:UICKeyChainStoreAccessibilityWhenPasscodeSetThisDeviceOnly
authenticationPolicy:UICKeyChainStoreAuthenticationPolicyUserPresence];
keychain.authenticationPrompt = @"Authenticate to update your access token";
NSString *token = keychain[@"kishikawakatsumi"];
}); Removing a Touch ID protected itemThe same way as when you remove a normal item. There is no way to show Touch ID or passcode authentication when removing Keychain items. UICKeyChainStore *keychain = [UICKeyChainStore keyChainStoreWithService:@"com.example.github-token"];
keychain[@"kishikawakatsumi"] = nil; Shared Web Credentials
UICKeyChainStore *keychain = [UICKeyChainStore keyChainStoreWithServer:[NSURL URLWithString:@"https://kishikawakatsumi.com"]
protocolType:UICKeyChainStoreProtocolTypeHTTPS];
NSString *username = @"[email protected]";
NSString *password = keychain[username];
if (password) {
// If found password in the Keychain,
// then log into the server
} else {
// If not found password in the Keychain,
// try to read from Shared Web Credentials
[keychain sharedPasswordForAccount:username completion:^(NSString *password, NSError *error) {
if (password) {
// If found password in the Shared Web Credentials,
// then log into the server
// and save the password to the Keychain
keychain[username] = password
} else {
// If not found password either in the Keychain also Shared Web Credentials,
// prompt for username and password
// Log into server
// If the login is successful,
// save the credentials to both the Keychain and the Shared Web Credentials.
keychain[username] = password
[keychain setSharedPassword:password forAccount:username completion:nil];
}
}];
} Request all associated domain's credentials[UICKeyChainStore requestSharedWebCredentialWithCompletion:^(NSArray *credentials, NSError *error) {
}]; Generate strong random passwordGenerate strong random password that is in the same format used by Safari autofill (xxx-xxx-xxx-xxx). NSString *password = [UICKeyChainStore generatePassword];
NSLog(@"%@", password); // => Nhu-GKm-s3n-pMx |