-
Notifications
You must be signed in to change notification settings - Fork 371
Description
I needed notifications of when a login had successfully occurred, so I've written that feature. The below code will post notifications to the NotificationCenter with the name @"SHKLogin", with the userInfo dictionary containing the class name of the sharer logged in under the key @"sharerId".
I believe I've put them in the best places to ensure it gets called only when a successful login event occurs, but if there is a better place, please let me know.
This requires changes to both SHKSharer and SHKOAuthSharer classes, as well as to the SHKFacebook class, as it uses a different login mechanism.
I have tested this with Delicious, Facebook (not with proxy), Google Reader, Pinboard, Read It Later, Tumblr, and Twitter. I did not test Evernote, as I have not yet gotten that sharer working.
EDIT: DRYed code.
SHKSharer.h:
-(void)postAuthorizationNotification;
SHKSharer.m:
-(void)postAuthorizationNotification {
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:[self sharerId] forKey:@"sharerId"];
NSNotification *note = [NSNotification notificationWithName:@"SHKLogin" object:self userInfo:userInfo];
[[NSNotificationCenter defaultCenter] postNotification:note];
}
- (void)authorizationFormSave:(SHKFormController *)form {
// -- Save values
NSDictionary *formValues = [form formValues];
NSString *value;
NSString *sharerId = [self sharerId];
NSArray *fields = [[[form sections] objectAtIndex:0] objectForKey:@"rows"];
for (SHKFormFieldSettings *field in fields) {
value = [formValues objectForKey:field.key];
[SHK setAuthValue:value forKey:field.key forSharer:sharerId];
}
[self postAuthorizationNotification];
// -- Try to share again
if ( [self validateItem] ) //only share if we have an item - wont have item if only logging in
[self share];
}
SHKOAuthSharer.m:
- (void)storeAccessToken {
[SHK setAuthValue:accessToken.key
forKey:@"accessKey"
forSharer:[self sharerId]];
[SHK setAuthValue:accessToken.secret
forKey:@"accessSecret"
forSharer:[self sharerId]];
[SHK setAuthValue:accessToken.sessionHandle
forKey:@"sessionHandle"
forSharer:[self sharerId]];
[self postAuthorizationNotification];
}
SHKFacebook.m:
- (void)session:(FBSession*)session didLogin:(FBUID)uid {
// Try to share again
if (pendingFacebookAction == SHKFacebookPendingLogin) {
[self postAuthorizationNotification];
self.pendingFacebookAction = SHKFacebookPendingNone;
[self share];
}
}