Last active
August 29, 2015 13:57
-
-
Save clooth/9828850 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
// | |
// AccountsViewController.h | |
// | |
// Created by Nico Hämäläinen on 28/03/14. | |
// | |
#import <UIKit/UIKit.h> | |
@interface AccountsViewController : UIViewController | |
@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
// | |
// AccountsViewController.m | |
// | |
// Created by Nico Hämäläinen on 28/03/14. | |
// | |
#import "AccountsViewController.h" | |
#import <Accounts/Accounts.h> | |
@interface AccountsViewController () <UIActionSheetDelegate> | |
@property (strong, nonatomic) ACAccountStore *accountStore; | |
@property (strong, nonatomic) ACAccountType *accountType; | |
@property (strong, nonatomic) NSArray *availableAccounts; | |
@property (strong, nonatomic) NSMutableArray *availableAccountNames; | |
@property (strong, nonatomic) ACAccount *chosenAccount; | |
@end | |
@implementation AccountsViewController | |
- (void)viewDidLoad { | |
// Needed to access the accounts on the device | |
_accountStore = [[ACAccountStore alloc] init]; | |
// Needed to filter only Twitter accounts | |
_accountType = [_accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter]; | |
// Request access to the Twitter accounts on the device | |
// NOTE: This pops up the alert to ask for permission | |
[_accountStore requestAccessToAccountsWithType:_accountType options:nil completion: | |
^(BOOL granted, NSError *error) { | |
// We can only continue if the user | |
if (granted == YES) { | |
// Get the list of accounts we found | |
_availableAccounts = [_accountStore accountsWithAccountType:_accountType]; | |
// If we have any, we'll save them and let the user pick the correct one | |
if ([_availableAccounts count] > 0) { | |
// Pop up dat picker sheet! | |
[self displayAccountSelector]; | |
} | |
else { | |
// Show an alert | |
[self displayAlertView]; | |
} | |
} | |
}]; | |
[super viewDidLoad]; | |
} | |
- (void)displayAccountSelector { | |
// Localized button titles | |
NSString *pickerTitle = NSLocalizedString(@"Pick an Account", nil); | |
NSString *cancelTitle = NSLocalizedString(@"Cancel", nil); | |
// Create an empty action sheet with our title | |
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:pickerTitle | |
delegate:self | |
cancelButtonTitle:nil | |
destructiveButtonTitle:nil | |
otherButtonTitles:nil]; | |
// Enumerate account names to display in the picker | |
for (ACAccount *account in _availableAccounts) { | |
[actionSheet addButtonWithTitle:[account username]]; | |
} | |
// Add cancel button | |
[actionSheet addButtonWithTitle:cancelTitle]; | |
[actionSheet setCancelButtonIndex:[_availableAccounts count]]; | |
// Display | |
[actionSheet showInView:self.view]; | |
} | |
- (void)displayAlertView { | |
NSString *alertTitle = NSLocalizedString(@"Error", nil); | |
NSString *alertMessage = NSLocalizedString(@"No Twitter accounts found.", nil); | |
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:alertTitle | |
message:alertMessage | |
delegate:nil | |
cancelButtonTitle:nil | |
otherButtonTitles:nil]; | |
[alertView show]; | |
} | |
#pragma mark - UIActionSheetDelegate | |
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { | |
ACAccount *chosenAccount = _availableAccounts[buttonIndex]; | |
if (chosenAccount != nil) { | |
_chosenAccount = chosenAccount; | |
} | |
// Do whatever you want here. | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment