Permalink
Newer
Older
100644 88 lines (57 sloc) 3.52 KB
1
# SAMKeychain
Apr 6, 2010
2
Sep 27, 2016
3
[![Version](https://img.shields.io/github/release/soffes/SAMKeychain.svg)](https://github.com/soffes/SAMKeychain/releases)
4
[![CocoaPods](https://img.shields.io/cocoapods/v/SAMKeychain.svg)](https://cocoapods.org/pods/SAMKeychain)
5
[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)
6
7
SAMKeychain is a simple wrapper for accessing accounts, getting passwords, setting passwords, and deleting passwords using the system Keychain on Mac OS X and iOS.
Mar 21, 2013
9
## Adding to Your Project
11
Simply add the following to your Podfile if you're using CocoaPods:
12
13
``` ruby
14
pod 'SAMKeychain'
15
```
16
17
or Cartfile if you're using Carthage:
18
19
```
Jul 25, 2016
20
github "soffes/SAMKeychain"
23
To manually add to your project:
24
25
1. Add `Security.framework` to your target
26
2. Add `SAMKeychain.h`, `SAMKeychain.m`, `SAMKeychainQuery.h`, and `SAMKeychainQuery.m` to your project.
28
SAMKeychain requires ARC.
May 8, 2012
29
30
Note: Currently SAMKeychain does not support Mac OS 10.6.
Mar 21, 2013
32
## Working with the Keychain
34
SAMKeychain has the following class methods for working with the system keychain:
36
```objective-c
37
+ (NSArray *)allAccounts;
38
+ (NSArray *)accountsForService:(NSString *)serviceName;
39
+ (NSString *)passwordForService:(NSString *)serviceName account:(NSString *)account;
40
+ (BOOL)deletePasswordForService:(NSString *)serviceName account:(NSString *)account;
41
+ (void)setAccessibilityType:(CFTypeRef)accessibilityType;
42
+ (BOOL)setPassword:(NSString *)password forService:(NSString *)serviceName account:(NSString *)account;
43
```
Jul 25, 2016
45
Easy as that. (See [SAMKeychain.h](https://github.com/soffes/samkeychain/blob/master/Sources/SAMKeychain.h) and [SAMKeychainQuery.h](https://github.com/soffes/samkeychain/blob/master/Sources/SAMKeychainQuery.h) for all of the methods.)
Feb 10, 2011
46
Oct 18, 2011
48
## Documentation
49
50
### Use prepared documentation
51
52
Read the [online documentation](http://cocoadocs.org/docsets/SAMKeychain).
53
Feb 10, 2011
54
## Debugging
55
56
If your saving to the keychain fails, use the NSError object to handle it. You can invoke `[error code]` to get the numeric error code. A few values are defined in SAMKeychain.h, and the rest in SecBase.h.
Feb 10, 2011
57
58
```objective-c
59
NSError *error = nil;
60
SAMKeychainQuery *query = [[SAMKeychainQuery alloc] init];
Mar 21, 2013
61
query.service = @"MyService";
62
query.account = @"soffes";
63
[query fetch:&error];
Mar 21, 2013
65
if ([error code] == errSecItemNotFound) {
66
NSLog(@"Password not found");
Mar 21, 2013
68
NSLog(@"Some other error occurred: %@", [error localizedDescription]);
69
}
70
```
Feb 10, 2011
71
Mar 21, 2013
72
Obviously, you should do something more sophisticated. You can just call `[error localizedDescription]` if all you need is the error message.
73
74
## Disclaimer
75
76
Working with the keychain is pretty sucky. You should really check for errors and failures. This library doesn't make it any more stable, it just wraps up all of the annoying C APIs.
77
78
You also really should not use the default but set the `accessibilityType`.
79
`kSecAttrAccessibleWhenUnlocked` should work for most applications. See
80
[Apple Documentation](https://developer.apple.com/library/ios/DOCUMENTATION/Security/Reference/keychainservices/Reference/reference.html#//apple_ref/doc/constant_group/Keychain_Item_Accessibility_Constants)
81
for other options.
Mar 21, 2013
82
83
## Thanks
84
85
This was originally inspired by EMKeychain and SDKeychain (both of which are now gone). Thanks to the authors. SAMKeychain has since switched to a simpler implementation that was abstracted from [SSToolkit](https://github.com/soffes/sstoolkit).
Mar 21, 2013
86
87
A huge thanks to [Caleb Davenport](https://github.com/calebd) for leading the way on version 1.0 of SAMKeychain.