--- a/Classes/SFHFKeychainUtils.h | |
+++ b/Classes/SFHFKeychainUtils.h | |
@@ -35,7 +35,7 @@ | |
} | |
+ (NSString *) getPasswordForUsername: (NSString *) username andServiceName: (NSString *) serviceName error: (NSError **) error; | |
-+ (void) storeUsername: (NSString *) username andPassword: (NSString *) password forServiceName: (NSString *) serviceName updateExisting: (BOOL) updateExisting error: (NSError **) error; | |
-+ (void) deleteItemForUsername: (NSString *) username andServiceName: (NSString *) serviceName error: (NSError **) error; | |
++ (BOOL) storeUsername: (NSString *) username andPassword: (NSString *) password forServiceName: (NSString *) serviceName updateExisting: (BOOL) updateExisting error: (NSError **) error; | |
++ (BOOL) deleteItemForUsername: (NSString *) username andServiceName: (NSString *) serviceName error: (NSError **) error; | |
@end | |
\ No newline at end of file | |
--- a/Classes/SFHFKeychainUtils.m | |
+++ b/Classes/SFHFKeychainUtils.m | |
@@ -286,19 +286,23 @@ | |
return [password autorelease]; | |
} | |
-+ (void) storeUsername: (NSString *) username andPassword: (NSString *) password forServiceName: (NSString *) serviceName updateExisting: (BOOL) updateExisting error: (NSError **) error { | |
- if (!username || !password || !serviceName) { | |
- if (error != nil) { | |
++ (BOOL) storeUsername: (NSString *) username andPassword: (NSString *) password forServiceName: (NSString *) serviceName updateExisting: (BOOL) updateExisting error: (NSError **) error | |
+{ | |
+ if (!username || !password || !serviceName) | |
+ { | |
+ if (error != nil) | |
+ { | |
*error = [NSError errorWithDomain: SFHFKeychainUtilsErrorDomain code: -2000 userInfo: nil]; | |
} | |
- return; | |
+ return NO; | |
} | |
// See if we already have a password entered for these credentials. | |
NSError *getError = nil; | |
NSString *existingPassword = [SFHFKeychainUtils getPasswordForUsername: username andServiceName: serviceName error:&getError]; | |
- if ([getError code] == -1999) { | |
+ if ([getError code] == -1999) | |
+ { | |
// There is an existing entry without a password properly stored (possibly as a result of the previous incorrect version of this code. | |
// Delete the existing item before moving on entering a correct one. | |
@@ -306,31 +310,38 @@ | |
[self deleteItemForUsername: username andServiceName: serviceName error: &getError]; | |
- if ([getError code] != noErr) { | |
- if (error != nil) { | |
+ if ([getError code] != noErr) | |
+ { | |
+ if (error != nil) | |
+ { | |
*error = getError; | |
} | |
- return; | |
+ return NO; | |
} | |
} | |
- else if ([getError code] != noErr) { | |
- if (error != nil) { | |
+ else if ([getError code] != noErr) | |
+ { | |
+ if (error != nil) | |
+ { | |
*error = getError; | |
} | |
- return; | |
+ return NO; | |
} | |
- if (error != nil) { | |
+ if (error != nil) | |
+ { | |
*error = nil; | |
} | |
OSStatus status = noErr; | |
- if (existingPassword) { | |
+ if (existingPassword) | |
+ { | |
// We have an existing, properly entered item with a password. | |
// Update the existing item. | |
- if (![existingPassword isEqualToString:password] && updateExisting) { | |
+ if (![existingPassword isEqualToString:password] && updateExisting) | |
+ { | |
//Only update if we're allowed to update existing. If not, simply do nothing. | |
NSArray *keys = [[[NSArray alloc] initWithObjects: (NSString *) kSecClass, | |
@@ -350,7 +361,8 @@ | |
status = SecItemUpdate((CFDictionaryRef) query, (CFDictionaryRef) [NSDictionary dictionaryWithObject: [password dataUsingEncoding: NSUTF8StringEncoding] forKey: (NSString *) kSecValueData]); | |
} | |
} | |
- else { | |
+ else | |
+ { | |
// No existing entry (or an existing, improperly entered, and therefore now | |
// deleted, entry). Create a new entry. | |
@@ -373,21 +385,30 @@ | |
status = SecItemAdd((CFDictionaryRef) query, NULL); | |
} | |
- if (error != nil && status != noErr) { | |
+ if (error != nil && status != noErr) | |
+ { | |
// Something went wrong with adding the new item. Return the Keychain error code. | |
*error = [NSError errorWithDomain: SFHFKeychainUtilsErrorDomain code: status userInfo: nil]; | |
+ | |
+ return NO; | |
} | |
+ | |
+ return YES; | |
} | |
-+ (void) deleteItemForUsername: (NSString *) username andServiceName: (NSString *) serviceName error: (NSError **) error { | |
- if (!username || !serviceName) { | |
- if (error != nil) { | |
++ (BOOL) deleteItemForUsername: (NSString *) username andServiceName: (NSString *) serviceName error: (NSError **) error | |
+{ | |
+ if (!username || !serviceName) | |
+ { | |
+ if (error != nil) | |
+ { | |
*error = [NSError errorWithDomain: SFHFKeychainUtilsErrorDomain code: -2000 userInfo: nil]; | |
} | |
- return; | |
+ return NO; | |
} | |
- if (error != nil) { | |
+ if (error != nil) | |
+ { | |
*error = nil; | |
} | |
@@ -398,9 +419,14 @@ | |
OSStatus status = SecItemDelete((CFDictionaryRef) query); | |
- if (error != nil && status != noErr) { | |
+ if (error != nil && status != noErr) | |
+ { | |
*error = [NSError errorWithDomain: SFHFKeychainUtilsErrorDomain code: status userInfo: nil]; | |
+ | |
+ return NO; | |
} | |
+ | |
+ return YES; | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment