Created
January 20, 2013 23:01
-
-
Save stith/4582429 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@implementation Observer | |
-(id)init { | |
// ... | |
[_preferences addObserver:self forKeyPath:@"apiKey" options:NSKeyValueObservingOptionNew context:nil]; | |
[_preferences addObserver:self forKeyPath:@"apiSecret" options:NSKeyValueObservingOptionNew context:nil]; | |
} | |
// ... | |
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { | |
if ([keyPath isEqualToString:@"apiKey"]) { | |
_apiKey = [change objectForKey:NSKeyValueChangeNewKey]; | |
} else if ([keyPath isEqualToString:@"apiSecret"]) { | |
_apiSecret = [change objectForKey:NSKeyValueChangeNewKey]; | |
} | |
MSLog(@"Secret: %@", _apiSecret); | |
MSLog(@"Change: %@:%@", keyPath, changed); | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@class EMGenericKeychainItem; | |
@interface Preferences : NSObject { | |
@private | |
EMGenericKeychainItem *_keychainItem; | |
} | |
+ (id)preferences; | |
@property (nonatomic, retain) NSString *apiKey; | |
@property (nonatomic, retain) NSString *apiSecret; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@implementation Preferences | |
@synthesize apiKey = _apiKey; | |
@synthesize apiSecret = _apiSecret; | |
// ... | |
-(NSString*)apiSecret { | |
return _keychainItem.password; | |
} | |
-(void)apiSecret:(NSString *)newApiSecret { | |
MSLog(@"Changed: %@", newApiSecret); | |
if (newApiSecret && ![newApiSecret isKindOfClass:[NSNull class]]) { | |
_keychainItem.password = newApiSecret; | |
} else { | |
_keychainItem.password = @""; | |
} | |
_apiSecret = newApiSecret; | |
} | |
// ... | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment