Skip to content

Instantly share code, notes, and snippets.

@slmcmahon
Created August 20, 2010 13:01
Show Gist options
  • Save slmcmahon/540249 to your computer and use it in GitHub Desktop.
Save slmcmahon/540249 to your computer and use it in GitHub Desktop.
/*
This is a convenience class for storing login credentials
This depends on SFHFKeychainUtils from http://github.com/ldandersen/scifihifi-iphone/tree/master/security
and you must add Security.framework to your project
*/
/**********************************************
//CredentialManager.h
#import <Foundation/Foundation.h>
@interface CredentialManager : NSObject {}
+ (BOOL) setUserName:(NSString *)userName
andPassword:(NSString *)password
withUpdate:(BOOL)update
error:(NSError **) error;
+ (NSString *)getUserName;
+ (NSString *)getPasswordForUserName:(NSString *) userName
error:(NSError **) error;
@end
/**********************************************
//CredentialManager.m
#import "CredentialManager.h"
#import "SFHFKeychainUtils.h"
@implementation CredentialManager
+ (NSString *)getUserName {
return (NSString *)[[NSUserDefaults standardUserDefaults] objectForKey:@"userName"];
}
+ (BOOL)setUserName:(NSString *) userName
andPassword:(NSString *) password
withUpdate:(BOOL) update
error:(NSError **) error {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setValue:userName forKey:@"userName"];
[defaults synchronize];
return [SFHFKeychainUtils storeUsername:userName
andPassword:password
forServiceName:@"yourAppName"
updateExisting:update
error:error];
}
+ (NSString *)getPasswordForUserName:(NSString *) userName
error:(NSError **) error {
return [SFHFKeychainUtils getPasswordForUsername:userName
andServiceName:@"yourAppName"
error:error];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment