从iOS 5中的操作表中选择Twitter帐户时,EXC访问错误

时间:2014-01-29 09:22:31

标签: iphone objective-c ios5 nsarray exc-bad-access

我现在正在获取设备中配置的Twitter帐户并显示在操作表中,当我选择一个帐户时,我得到了错误的访问错误。它在我打印twitterArray

的actionsheet委托方法中崩溃
@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];
        }
    }
}

NSLog(@"Account:%@",twitterAccountsArray);在操作表方法和初始twitter方法中打印正常,但在操作表委托方法中崩溃。有任何想法吗。这种情况仅在iOS5中适用于iOS6及以上版本

1 个答案:

答案 0 :(得分:0)

我能够通过使accountStore和accountType全局

来解决问题
@interface LogInViewController ()
{
    NSArray *twitterAccountsArray;
    ACAccount *selectedAccount;
    ACAccountStore *accountStore;
    ACAccountType *accountType;
}