Skip to content

Instantly share code, notes, and snippets.

@asciimike
Created July 31, 2014 17:57
Show Gist options
  • Save asciimike/c50b8810b95c51ccc2c0 to your computer and use it in GitHub Desktop.
Save asciimike/c50b8810b95c51ccc2c0 to your computer and use it in GitHub Desktop.
Snippet of code for handling Twitter multiple login using Firebase Simple Login
@interface TwitterTest()
@property (strong, nonatomic) NSString *currentTwitterHandle;
@end
@implementation TwitterTest // Note, must conform to the <UIActionSheetDelegate> protocol
- (void)authenticateWithTwitter;
{
if (self.currentTwitterHandle) {
// If the user has a currently selected twitter handle, log them in normally
Firebase *ref = [[Firebase alloc] initWithUrl:@"https://<your-firebase>.firebaseio.com"];
FirebaseSimpleLogin *authRef = [[FirebaseSimpleLogin alloc] initWithRef:ref];
[authRef loginToTwitterAppWithId:@"TWITTER_APP_ID" multipleAccountsHandler:^int(NSArray *usernames) {
return (int)[usernames indexOfObject:self.currentTwitterHandle];
} withCompletionBlock:^(NSError *error, FAUser *user) {
if (error != nil) {
// An error has occurred!
} else {
// We have a user
self.title = self.currentTwitterHandle;
}
}];
} else {
// Access account store to pull twitter accounts on device
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error) {
if (granted) {
NSArray *accounts = [accountStore accountsWithAccountType:accountType];
[self handleMultipleTwitterAccounts:accounts];
}
}];
}
}
- (void)handleMultipleTwitterAccounts:(NSArray *)accounts;
{
switch ([accounts count]) {
case 0:
// Deal with setting up a twitter account (could also not be set up on the device, deal with this differently)
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://twitter.com/signup"]];
break;
case 1:
// Single user system, go straight to login
self.currentTwitterHandle = [[accounts firstObject] username];
[self authenticateWithTwitter];
break;
default:
// Handle multiple users
[self selectTwitterAccount:accounts];
break;
}
}
- (void)selectTwitterAccount:(NSArray *)accounts;
{
// Pop up action sheet which has different user accounts as options
UIActionSheet *selectUserActionSheet = [[UIActionSheet alloc] initWithTitle:@"Select Twitter Account" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles: nil];
for (ACAccount *account in accounts) {
[selectUserActionSheet addButtonWithTitle:[account username]];
}
selectUserActionSheet.cancelButtonIndex = [selectUserActionSheet addButtonWithTitle:@"Cancel"];
[selectUserActionSheet showInView:self.view];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex;
{
self.currentTwitterHandle = [actionSheet buttonTitleAtIndex:buttonIndex];
[self authenticateWithTwitter];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment