My e-mail: [email protected] If you have this article to help you point under the recommendation or feel free to comment on a chant, thank you thank you, casually reproduced, indicating the source is good.
4.Keychain use? ---to maximize utility I think I should just say it first!
Of course, using a third-party library: Sskeychain 3000+ Star Library no kidding. GitHub Address: Https://github.com/soffes/sskeychain
After importing, first, compile the error.
If you are importing manually:
1. Copy the SSKeychain.h sskeychain.m SSKeychainQuery.h sskeychainquery.m to the project
2. Add security.framework how to add? Click on the +
3.sskeychain.h wrong? Change the #import <SSKeychain/SSKeychainQuery.h> in SSKeychain.h to #import <foundation/ foundation.h> #import "SSKeychainQuery.h".
Still wrong? As a small white I do not know, send me an email to discuss it.
Next 4 procedures are shown
Basic Note: The stored data has three 1. Service name (this is convenient for the account password classification) 2. Account 3. Password and all three data are nsstring (if you want to save other types, look at the back)
The API used:
Add and update all with this: + (BOOL) SetPassword: (nsstring *) password Forservice: (nsstring *) serviceName Account: (nsstring *) account;
Search Password: + (nsstring *) Passwordforservice: (nsstring *) serviceName account: (nsstring *) account;
Delete: + (BOOL)deletepasswordforservice: (nsstring *) serviceName account: ( nsstring *) account;
1. Add a key (the information of this key consists of the service name + account + password)
Remember to add a header file
#import "SSKeychain.h"
#import "SSKeychainQuery.h"
Define the stuff you want to use first.
nsstring * ServiceName = @ "Com.keychaintest.data";
nsstring *account = @ "M4ABCD";
nsstring *password = @ "12345678";
Add a keychain!
if ([sskeychain setpassword:p assword forservice: serviceName Account : Account]) {
NSLog(@ "Success!") );
}
Description: It's so simple.
2. Enquiry
1. Query the password for count under a service and print it out:
NSLog(@ "%@", [sskeychain passwordforservice: ServiceName account: Account]);
2. Check all keys under service:
nsarray *keys = [sskeychain accountsforservice: serviceName];
Here is my output:
2016-03-04 15:08:43.785 keychaintest[31342:4403403] (
{
Acct = M4ABCD;
AGRP = test;
Cdat = "2016-03-03 07:10:58 +0000";
Mdat = "2016-03-04 07:08:43 +0000";
PDMN = AK;
Svce = "Com.keychaintest.data";
sync = 0;
Tomb = 0;
}
)
Description: The returned result is an array, the members of the arrays is the key to our query, there is only one key, and the key information is constructed in the form of a dictionary, the key acct is count, the key Svce is servicename. Where's the password? Use Method 1 to fetch it!
3. Check all the keys of the Appkeychain
nsarray *keys = [Sskeychain allaccounts];
3. Update
if ([sskeychain setpassword:@ "321321" forservice: ServiceName account: Account]) {
NSLog (@ "set success!");
}
4. Delete
if ([sskeychain deletepasswordforservice:servicename account:account]) {
NSLog (@ "Delete success!");
}
Note: Delete is to remove this key, oh, not just delete the password!
Additional instructions: If your password is NSData
Enquiry: + (nsdata *) Passworddataforservice: (nsstring *) serviceName account: (nsstring *) account;
To set or update: + (BOOL) Setpassworddata: (nsdata *) password Forservice: (nsstring *) serviceName Account: (nsstring *) account;
Let 's start with a shallow understanding and a little bit of explanation for the Apple API.
What is 1.Keychain?
1.keychain is the key locker! is a safe that Apple provides to us.
This article is for iOS only.
In iOS every app has its own keychain, the most commonly used is to save the user's account and password, that is, remember the password, put here is very safe (Apple is responsible for helping us to encrypt and save up, if the problem is strange to him!) If you save these secret data with Nsuserdefault, the generated plist file (placed under that library/preferences) is easy to get, and you have to encrypt it yourself.
2.Keychain composition?
1. The component consists of {N attribute key value pair + A password key value pair}!
2. Structure: The structure can be seen as a dictionary in the form of: @{@ "attribute Key1": @ "Property value 1" @ "Property Keyn": @ "Property value n" @ "password key" @ "Password value"}
Wait till I get back to writing!
iOS Keychain keychain--primary use + shallow understanding