Convert Figma logo to code with AI

soffes logoSAMKeychain

Simple Objective-C wrapper for the keychain that works on Mac and iOS

5,401
953
5,401
41

Top Related Projects

Simple Swift wrapper for Keychain that works on iOS, watchOS, tvOS and macOS.

Helper functions for saving text in Keychain securely for iOS, OS X, tvOS and watchOS.

3,991

Valet lets you securely store data in the iOS, tvOS, or macOS Keychain without knowing a thing about how the Keychain works. It’s easy. We promise.

Convenient & secure logging during development & release in Swift 4 & 5

Quick Overview

SAMKeychain is a simple Objective-C wrapper for accessing the iOS Keychain. It provides a straightforward interface for storing and retrieving passwords, keys, and other sensitive data securely on iOS devices. The library simplifies the process of working with the Keychain Services API, making it easier for developers to implement secure data storage in their applications.

Pros

  • Easy to use API with simple methods for common Keychain operations
  • Supports both iOS and macOS platforms
  • Well-maintained and actively supported by the community
  • Includes comprehensive documentation and examples

Cons

  • Limited to Objective-C and Swift projects
  • May not provide advanced Keychain features for complex use cases
  • Requires manual integration (no CocoaPods or Carthage support)
  • Potential security risks if not used properly (like any Keychain wrapper)

Code Examples

  1. Storing a password:
[SAMKeychain setPassword:@"secretPassword" forService:@"MyApp" account:@"user@example.com"];
  1. Retrieving a password:
NSString *password = [SAMKeychain passwordForService:@"MyApp" account:@"user@example.com"];
  1. Deleting a password:
[SAMKeychain deletePasswordForService:@"MyApp" account:@"user@example.com"];
  1. Checking if a password exists:
BOOL exists = [SAMKeychain passwordExistsForService:@"MyApp" account:@"user@example.com"];

Getting Started

  1. Download the SAMKeychain files from the GitHub repository.
  2. Add the SAMKeychain.h and SAMKeychain.m files to your Xcode project.
  3. Import the header file in your code:
#import "SAMKeychain.h"
  1. Start using SAMKeychain methods in your application:
// Store a password
[SAMKeychain setPassword:@"myPassword" forService:@"MyApp" account:@"user@example.com"];

// Retrieve a password
NSString *password = [SAMKeychain passwordForService:@"MyApp" account:@"user@example.com"];

// Use the retrieved password
if (password) {
    NSLog(@"Retrieved password: %@", password);
} else {
    NSLog(@"Password not found");
}

Competitor Comparisons

Simple Swift wrapper for Keychain that works on iOS, watchOS, tvOS and macOS.

Pros of KeychainAccess

  • More comprehensive API with support for additional Keychain features
  • Better Swift integration and syntax
  • Active development and maintenance

Cons of KeychainAccess

  • Larger codebase, potentially increasing app size
  • Steeper learning curve for simpler use cases

Code Comparison

SAMKeychain:

SAMKeychain.setPassword("secret", forService: "MyService", account: "user@example.com")
let password = SAMKeychain.password(forService: "MyService", account: "user@example.com")

KeychainAccess:

let keychain = Keychain(service: "MyService")
try keychain.set("secret", key: "user@example.com")
let password = try keychain.get("user@example.com")

Both libraries provide simple ways to interact with the Keychain, but KeychainAccess offers a more Swift-like syntax and error handling. SAMKeychain uses a more Objective-C style API, which may be familiar to developers transitioning from Objective-C. KeychainAccess provides additional features like access group support, synchronization options, and more granular control over Keychain attributes, making it more suitable for complex use cases.

Helper functions for saving text in Keychain securely for iOS, OS X, tvOS and watchOS.

Pros of keychain-swift

  • Written in Swift, providing better integration with modern iOS/macOS projects
  • Simpler API with fewer methods, making it easier to use for basic keychain operations
  • Supports value encoding, allowing storage of various data types beyond just strings

Cons of keychain-swift

  • Less comprehensive feature set compared to SAMKeychain
  • Lacks some advanced options for keychain item attributes
  • May not be as suitable for complex keychain management scenarios

Code Comparison

SAMKeychain:

[SAMKeychain setPassword:@"secret" forService:@"MyApp" account:@"user@example.com"];
NSString *password = [SAMKeychain passwordForService:@"MyApp" account:@"user@example.com"];

keychain-swift:

let keychain = KeychainSwift()
keychain.set("secret", forKey: "myKey")
if let value = keychain.get("myKey") {
    print("Value: \(value)")
}

Both libraries provide simple ways to store and retrieve keychain items, but keychain-swift uses a more Swift-idiomatic approach with a dedicated object and method chaining. SAMKeychain offers a class-based API with separate methods for different operations, which may be more familiar to Objective-C developers.

3,991

Valet lets you securely store data in the iOS, tvOS, or macOS Keychain without knowing a thing about how the Keychain works. It’s easy. We promise.

Pros of Valet

  • More comprehensive API with additional features like iCloud Keychain synchronization
  • Better support for modern Swift syntax and practices
  • Actively maintained with regular updates and improvements

Cons of Valet

  • Larger codebase and potentially more complex implementation
  • May have a steeper learning curve for beginners
  • Requires iOS 9.0+ or macOS 10.11+, while SAMKeychain supports older versions

Code Comparison

SAMKeychain:

[SAMKeychain setPassword:@"secret" forService:@"MyApp" account:@"user@example.com"];
NSString *password = [SAMKeychain passwordForService:@"MyApp" account:@"user@example.com"];

Valet:

let myValet = Valet.valet(with: Identifier(nonEmpty: "MyApp")!, accessibility: .whenUnlocked)
try? myValet.setString("secret", forKey: "user@example.com")
let password = try? myValet.string(forKey: "user@example.com")

Both libraries provide similar core functionality for securely storing and retrieving sensitive data. SAMKeychain offers a simpler, Objective-C based API, while Valet provides a more feature-rich, Swift-oriented solution. The choice between them depends on project requirements, target platforms, and developer preferences.

Convenient & secure logging during development & release in Swift 4 & 5

Pros of SwiftyBeaver

  • Designed specifically for Swift, offering a more native and idiomatic experience
  • Supports multiple logging destinations (console, file, cloud) out of the box
  • Provides advanced features like custom formatting and filtering

Cons of SwiftyBeaver

  • Focused solely on logging, unlike SAMKeychain which handles keychain operations
  • May be considered overkill for simple logging needs
  • Requires more setup and configuration compared to SAMKeychain's simplicity

Code Comparison

SAMKeychain (Objective-C):

[SAMKeychain setPassword:@"secret" forService:@"MyApp" account:@"username"];
NSString *password = [SAMKeychain passwordForService:@"MyApp" account:@"username"];

SwiftyBeaver (Swift):

let log = SwiftyBeaver.self
log.addDestination(ConsoleDestination())
log.info("Hello World")
log.warning("This is a warning")
log.error("An error occurred")

While both libraries serve different purposes, this comparison highlights the key differences in their focus and implementation. SAMKeychain provides a simple interface for keychain operations, while SwiftyBeaver offers a comprehensive logging solution with multiple destinations and advanced features. The code examples demonstrate the distinct functionalities of each library.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

SAMKeychain

Version CocoaPods Carthage compatible

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:

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:

+ (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 and SAMKeychainQuery.h for all of the methods.)

Documentation

Use prepared documentation

Read the online documentation.

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.

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 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.

A huge thanks to Caleb Davenport for leading the way on version 1.0 of SAMKeychain.