The iOS Keychain service provides a secure way to save private information (passwords, serial numbers, certificates, and so on), and each iOS program has a separate keychain store. Compared to nsuserdefaults, file preservation and other general way, keychain save more secure, and keychain saved information will not be deleted because the app is lost, so after reloading the app, keychain data can also be used. From iOS 3. Starting with 0, it became possible to share keychain across programs.
Sskeychains has a simple encapsulation of the Apple Security Framework API, which supports access to passwords stored in keychain, accounts, including read, delete, and settings. Sskeychain's author is Samsoffes, author of the famous Sstoolkit.
Project Address: Https://github.com/samsoffes/sskeychain
Adding Sskeychain to the project
- Add the Security.framework framework to the project.
- Add SSKeychain.h and SSKEYCHAIN.M to the project folder.
Using Sskeychain
Use Sskeychain with the following class methods (see SSKeyChain.h):
+ (Nsarray *) allaccounts;
+ (Nsarray *) Accountsforservice: (NSString *) serviceName;
+ (NSString *) Passwordforservice: (NSString *) Servicenameaccount: (NSString *) account;
+ (BOOL) Deletepasswordforservice: (NSString *) Servicenameaccount: (NSString *) account;
+ (BOOL) SetPassword: (NSString *) password Forservice: (nsstring*) serviceName account: (NSString *) account;
Document
Installing Sskeychain's Help documentation in Xcode requires the following steps:
- Open Menu Xcode--Preferences
- Select Downloads
- Select documentation
- Click the plus button at the bottom and enter the following Url:http://docs.samsoff.es/com.samsoffes.sskeychain.atom
- Click the Install button next to "Sskeychain documentation". (If you don't see it, and no errors are prompted, restart Xcode)
Be sure to see Sskeychain in the optional Docset in organizer.
In addition, Sskeychain documentation can be viewed online .
Debugging
If you cannot save the keychain, use the error code provided in SSKeychain.h, for example:
Nserror *error = nil;
NSString *password = [sskeychainpasswordforservice:@ "MyService" account:@ "Samsoffes" error:&error];
if ([error code] = = Sskeychainerrornotfound) {
NSLog (@ "Passwordnot found");
}
Obviously, you should be familiar with doing this. Accessing the keychain is a painful thing, and you should always check it for every mistake and failure. Sskeychain does not make it more stable (keychain), it is just a cumbersome C APIs package.
Sample code
Save a UUID string to the keychain:
Cfuuidref uuid = cfuuidcreate (null); assert (uuid! = NULL); Cfstringref uuidstr = cfuuidcreatestring (NULL, UUID);
[Sskeychain setpassword: [NSString stringwithformat:@ "%@", Uuidstr]
forservice:@ "Com.yourapp.yourcompany" account:@ "user";
Then, read the UUID from the keychain:
NSString *retrieveuuid = [sskeychainpasswordforservice:@ "Com.yourapp.yourcompany" account:@ "user"];
Note: The Services and Accounts parameters in the SetPassword and Passwordforsevice methods
should be consistent.