I’m trying to implement a way for users to pick from the Twitter accounts they’ve set up on the iPhone (iOS5 and up). I can get the usernames to appear in a UIActionSheet, but for some reason the UIActionSheet takes about 5 seconds to appear after the method has been called.
I thought perhaps it was because it took a while to retrieve the list of Twitter accounts, but in my log they appear instantly, so it’s not that.
Any ideas?
- (void)TwitterSwitch {
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
// Create an account type that ensures Twitter accounts are retrieved.
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
NSMutableArray *buttonsArray = [NSMutableArray array];
// Request access from the user to use their Twitter accounts.
[accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) {
// Get the list of Twitter accounts.
NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];
NSLog(@"%@", accountsArray);
[accountsArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[buttonsArray addObject:((ACAccount*)obj).username];
}];
NSLog(@"%@", buttonsArray);
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
for( NSString *title in buttonsArray)
[actionSheet addButtonWithTitle:title];
[actionSheet addButtonWithTitle:@"Cancel"];
actionSheet.cancelButtonIndex = actionSheet.numberOfButtons-1;
[actionSheet showFromTabBar:self.tabBarController.tabBar];
}];
}
I think the problem might be that in requestAccessToAccountsWithType:withCompletionHandler: the handler can be called on an arbitrary queue, and you’re showing a UI element (the action sheet) from within that handler. Since interaction with the UI should only be done on the main thread, that could be a problem. I tried moving some of the code into another method, and calling it on the main thread — this was much faster:
Note that I changed the way I show the sheet because in my test app I didn’t have a tab bar controller.