Method one:

The keychain method was discovered today while doing offline operations for the networked version that required local authentication of passwords and usernames. You can quickly store and read what you want. Now let’s simply apply it together!

Address: www.lvtao.net/ios/ios-key…

The ios keychain service provides a secure way to store private information (passwords, serial numbers, certificates, etc.). Each ios application has a separate keychain store. Since ios 3.0, keychain sharing across applications has become possible.

The following uses keychain to access user names and passwords.

Apple has a ready-made classes encapsulate good keychain, KeychainItemWrapper. H and KeychainItemWrapper. M file, can be found in GenericKeychain instance.

But here I just need to access the username and password, instead of the class provided by Apple, write a simple class to implement.

The code is as follows:

CHKeychain.h

#import

#import

@interface CHKeychain : NSObject

+ (void)save:(NSString *)service data:(id)data;

+ (id)load:(NSString *)service;

+ (void)delete:(NSString *)service;

@end

CHKeychain.m

#import “CHKeychain.h”

@implementation CHKeychain

+ (NSMutableDictionary *)getKeychainQuery:(NSString *)service {

return [NSMutableDictionary dictionaryWithObjectsAndKeys:

(id)kSecClassGenericPassword,(id)kSecClass,

service, (id)kSecAttrService,

service, (id)kSecAttrAccount,

(id)kSecAttrAccessibleAfterFirstUnlock,(id)kSecAttrAccessible,

nil];

}

+ (void)save:(NSString *)service data:(id)data {

//Get search dictionary

NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];

//Delete old item before add new item

SecItemDelete((CFDictionaryRef)keychainQuery);

//Add new object to search dictionary(Attention:the data format)

[keychainQuery setObject:[NSKeyedArchiver archivedDataWithRootObject:data] forKey:(id)kSecValueData];

//Add item to keychain with the search dictionary

SecItemAdd((CFDictionaryRef)keychainQuery, NULL);

}

+ (id)load:(NSString *)service {

id ret = nil;

NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];

//Configure the search setting

//Since in our simple case we are expecting only a single attribute to be returned (the password) we can set the attribute kSecReturnData to kCFBooleanTrue

[keychainQuery setObject:(id)kCFBooleanTrue forKey:(id)kSecReturnData];

[keychainQuery setObject:(id)kSecMatchLimitOne forKey:(id)kSecMatchLimit];

CFDataRef keyData = NULL;

if (SecItemCopyMatching((CFDictionaryRef)keychainQuery, (CFTypeRef *)&keyData) == noErr) {

@try {

ret = [NSKeyedUnarchiver unarchiveObjectWithData:(NSData *)keyData];

} @catch (NSException *e) {

NSLog(@”Unarchive of %@ failed: %@”, service, e);

} @finally {

}

}

if (keyData)

CFRelease(keyData);

return ret;

}

+ (void)delete:(NSString *)service {

NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];

SecItemDelete((CFDictionaryRef)keychainQuery);

}

@end

First we need to define a few strings for keys:

NSString * const KEY_USERNAME_PASSWORD = @”com.company.app.usernamepassword”;

NSString * const KEY_USERNAME = @”com.company.app.username”;

NSString * const KEY_PASSWORD = @”com.company.app.password”;

Add the user name and password to the keychain:

NSMutableDictionary *usernamepasswordKVPairs = [NSMutableDictionary dictionary];

[usernamepasswordKVPairs setObject:txtfldUsername.text forKey:KEY_USERNAME];

[usernamepasswordKVPairs setObject:txtfldPassword.text forKey:KEY_PASSWORD];

[CHKeychain save:KEY_USERNAME_PASSWORD data:usernamepasswordKVPairs];

To obtain the user name and password from the keychain:

NSMutableDictionary *usernamepasswordKVPairs = (NSMutableDictionary *)[CHKeychain load:KEY_USERNAME_PASSWORD];

txtfldUsername.text = [usernamepasswordKVPairs objectForKey:KEY_USERNAME];

txtfldPassword.text = [usernamepasswordKVPairs objectForKey:KEY_PASSWORD];

Delete a keychain item:

[CHKeychain delete:KEY_USERNAME_PASSWORD];

Such a simple keychain access user name and password function is ready.

Method 2:

Use sandboxes to save information

This method is simpler and faster than the previous method, and there is no need to create a class. Just save it and take it out.

// Retrieve the saved name and password

_userNameTF.text = [[NSUserDefaults standardUserDefaults] objectForKey:kUserName];

_passWordTF.text = [[NSUserDefaults standardUserDefaults] objectForKey:kPassWd];

[[NSUserDefaults standardUserDefaults] synchronize];

// Save the name and password

[[NSUserDefaults standardUserDefaults] setObject:_userNameTF.text forKey:kUserName];

[[NSUserDefaults standardUserDefaults] setObject:_passWordTF.text forKey:kPassWd];

[[NSUserDefaults standardUserDefaults] synchronize];