I was able to solve the issue by making accountStore and accountType global
@interface LogInViewController ()
{
NSArray *twitterAccountsArray;
ACAccount *selectedAccount;
ACAccountStore *accountStore;
ACAccountType *accountType;
}
Question
I'm now currently fetching the twitter account configured in the device and display in action sheet and when I select a account I get exc bad access error. It crashes in the actionsheet delegate method where I print the twitterArray
@interface LogInViewController ()
{
NSArray *twitterAccountsArray;
ACAccount *selectedAccount;
}
-(void)getTwitterAccountsForLogin
{
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[accountStore requestAccessToAccountsWithType:accountType
withCompletionHandler:^(BOOL granted, NSError *error) {
if (granted && !error) {
twitterAccountsArray = [accountStore accountsWithAccountType:accountType];
NSLog(@"Account:%@",twitterAccountsArray);
if(twitterAccountsArray == nil || [twitterAccountsArray count] == 0)
{
showAlert(@"Message", TWITTER_NO_ACCOUNT_ERROR , STRING_CONSTANT_OK, nil);
}
else if ([twitterAccountsArray count] > 1)
{
[self performSelectorOnMainThread:@selector(accountListActionSheetDynamics:) withObject:twitterAccountsArray waitUntilDone:YES];
}
else
{
NSLog(@"Only one twitter account so login to that");
}
}
else
{
showAlert(@"Message", TWITTER_ACCESS_DENIED, STRING_CONSTANT_OK, nil);
}
}];
}
- (void)accountListActionSheetDynamics:(NSArray *) accounts {
UIActionSheet *accountsSheet = [[UIActionSheet alloc]
initWithTitle:@"Choose a Twitter Account"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
[accountsSheet setDelegate:self];
for(int i=0;i<accounts.count;i++)
{
[accountsSheet addButtonWithTitle:[[accounts objectAtIndex:i] valueForKey:@"username"]];
}
accountsSheet.cancelButtonIndex = [accountsSheet addButtonWithTitle:@"Cancel"];
[accountsSheet showFromRect:self.view.bounds inView:self.view animated:YES];
NSLog(@"Account:%@",twitterAccountsArray);
}
-(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == actionSheet.cancelButtonIndex)
{
return;
}
else
{
//Crashes here with exc bad access while printing log
NSLog(@"Account:%@",twitterAccountsArray);
for(int i=0;i<twitterAccountsArray.count;i++)
if([[actionSheet buttonTitleAtIndex:buttonIndex] caseInsensitiveCompare: [[twitterAccountsArray objectAtIndex:i] valueForKey:@"username"]]==NSOrderedSame)
{
selectedAccount = [twitterAccountsArray objectAtIndex:i];
}
}
}
The NSLog(@"Account:%@",twitterAccountsArray);
prints fine in the action sheet method and the initial twitter method but crashes in the action sheet delegate method. Any ideas. This happens only in iOS5 works fine for iOS6 and above
Solution
I was able to solve the issue by making accountStore and accountType global
@interface LogInViewController ()
{
NSArray *twitterAccountsArray;
ACAccount *selectedAccount;
ACAccountStore *accountStore;
ACAccountType *accountType;
}