Skip to content

Commit a559222

Browse files
committed
Add method to check if a key has a value
1 parent c81911f commit a559222

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

Example/Tests/A0SimpleKeychainSpec.m

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,18 @@
170170
[keychain setString:@"value1" forKey:key];
171171
});
172172

173+
it(@"should return that a nonexisting key doesnt exist", ^{
174+
expect([keychain stringForKey:@"SHOULDNOTEXIST"]).to.beFalsy();
175+
});
176+
177+
it(@"should return that a nil key doesnt exist", ^{
178+
expect([keychain stringForKey:nil]).to.beFalsy();
179+
});
180+
181+
it(@"should return that a key exists", ^{
182+
expect([keychain stringForKey:key]).to.beTruthy();
183+
});
184+
173185
it(@"should return nil string with nil key", ^{
174186
expect([keychain stringForKey:nil]).to.beNil();
175187
});

Pod/Classes/A0SimpleKeychain.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,15 @@ typedef NS_ENUM(NSInteger, A0SimpleKeychainItemAccessible) {
219219
*/
220220
- (NSData *)dataForKey:(NSString *)key promptMessage:(NSString *)message;
221221

222+
/**
223+
* Checks if a key has a value in the Keychain
224+
*
225+
* @param key the key to check if it has a value
226+
*
227+
* @return if the key has an associated value in the Keychain or not.
228+
*/
229+
- (BOOL)hasValueForKey:(NSString *)key;
230+
222231
///---------------------------------------------------
223232
/// @name Create helper methods
224233
///---------------------------------------------------

Pod/Classes/A0SimpleKeychain.m

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,15 @@ - (NSData *)dataForKey:(NSString *)key promptMessage:(NSString *)message {
8585
return dataFound;
8686
}
8787

88+
- (BOOL)hasValueForKey:(NSString *)key {
89+
if (key) {
90+
return NO;
91+
}
92+
NSDictionary *query = [self queryFindByKey:key message:nil];
93+
OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef)query, NULL);
94+
return status == errSecSuccess;
95+
}
96+
8897
- (BOOL)setString:(NSString *)string forKey:(NSString *)key {
8998
return [self setString:string forKey:key promptMessage:nil];
9099
}

0 commit comments

Comments
 (0)