Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/soffes/SAMKeychain
Simple Objective-C wrapper for the keychain that works on Mac and iOS
https://github.com/soffes/SAMKeychain
ios macos objective-c tvos watchos
Last synced: 3 months ago
JSON representation
Simple Objective-C wrapper for the keychain that works on Mac and iOS
- Host: GitHub
- URL: https://github.com/soffes/SAMKeychain
- Owner: soffes
- License: mit
- Archived: true
- Created: 2010-04-07T15:55:44.000Z (over 14 years ago)
- Default Branch: master
- Last Pushed: 2020-07-20T15:28:37.000Z (over 4 years ago)
- Last Synced: 2024-05-23T00:01:58.873Z (5 months ago)
- Topics: ios, macos, objective-c, tvos, watchos
- Language: Objective-C
- Homepage:
- Size: 245 KB
- Stars: 5,397
- Watchers: 166
- Forks: 955
- Open Issues: 41
-
Metadata Files:
- Readme: Readme.markdown
- License: LICENSE
Awesome Lists containing this project
- awesome-ios - SAMKeychain - Simple Objective-C wrapper for the keychain that works on Mac and iOS. (Security / Keychain)
- awesome-ios-star - SAMKeychain - Simple Objective-C wrapper for the keychain that works on Mac and iOS. (Security / Keychain)
- Awesome-iOS - SAMKeychain - Simple Objective-C wrapper for the keychain that works on Mac and iOS. (Database, ORM, Cache)
README
# SAMKeychain
[![Version](https://img.shields.io/github/release/soffes/SAMKeychain.svg)](https://github.com/soffes/SAMKeychain/releases)
[![CocoaPods](https://img.shields.io/cocoapods/v/SAMKeychain.svg)](https://cocoapods.org/pods/SAMKeychain)
[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)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.
## Adding to Your Project
Simply add the following to your Podfile if you're using CocoaPods:
``` ruby
pod 'SAMKeychain'
```or Cartfile if you're using Carthage:
```
github "soffes/SAMKeychain"
```To manually add to your project:
1. Add `Security.framework` to your target
2. Add `SAMKeychain.h`, `SAMKeychain.m`, `SAMKeychainQuery.h`, and `SAMKeychainQuery.m` to your project.SAMKeychain requires ARC.
Note: Currently SAMKeychain does not support Mac OS 10.6.
## Working with the Keychain
SAMKeychain has the following class methods for working with the system keychain:
```objective-c
+ (NSArray *)allAccounts;
+ (NSArray *)accountsForService:(NSString *)serviceName;
+ (NSString *)passwordForService:(NSString *)serviceName account:(NSString *)account;
+ (BOOL)deletePasswordForService:(NSString *)serviceName account:(NSString *)account;
+ (void)setAccessibilityType:(CFTypeRef)accessibilityType;
+ (BOOL)setPassword:(NSString *)password forService:(NSString *)serviceName account:(NSString *)account;
```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.)
## Documentation
### Use prepared documentation
Read the [online documentation](http://cocoadocs.org/docsets/SAMKeychain).
## Debugging
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.
```objective-c
NSError *error = nil;
SAMKeychainQuery *query = [[SAMKeychainQuery alloc] init];
query.service = @"MyService";
query.account = @"soffes";
[query fetch:&error];if ([error code] == errSecItemNotFound) {
NSLog(@"Password not found");
} else if (error != nil) {
NSLog(@"Some other error occurred: %@", [error localizedDescription]);
}
```Obviously, you should do something more sophisticated. You can just call `[error localizedDescription]` if all you need is the error message.
## Disclaimer
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.
You also really should not use the default but set the `accessibilityType`.
`kSecAttrAccessibleWhenUnlocked` should work for most applications. See
[Apple Documentation](https://developer.apple.com/library/ios/DOCUMENTATION/Security/Reference/keychainservices/Reference/reference.html#//apple_ref/doc/constant_group/Keychain_Item_Accessibility_Constants)
for other options.## Thanks
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).
A huge thanks to [Caleb Davenport](https://github.com/calebd) for leading the way on version 1.0 of SAMKeychain.