在iOS5中使用警报进行Twitter授权对话

时间:2012-02-29 15:40:22

标签: objective-c ios5 twitter alert

我正在使用iOS5 Twitter API获取用户使用其Twitter帐户的权限。弹出一个对话框,他们可以从中选择授予或拒绝该应用程序的权限。如果用户接受但没有在iPhone上设置Twitter帐户,我希望能够打开警报,但是由于已经打开了对话框,此时打开警报失败。如何解除Twitter权限对话后立即添加提醒?

- (IBAction)logInToTwitter:(id)sender
{
    //  First, we need to obtain the account instance for the user's Twitter account
    ACAccountStore *store = [[ACAccountStore alloc] init];

    ACAccountType *twitterAccountType = [store accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];

    [store requestAccessToAccountsWithType:twitterAccountType 
                     withCompletionHandler:^(BOOL granted, NSError *error) {
      if (granted) 
      {
        NSArray *twitterAccounts = [[store accountsWithAccountType:twitterAccountType] autorelease];

        if ([twitterAccounts count] > 0) 
        {
           //All good
        }
        else
        {
           //Open Alert
        }

      }
   }];
}

1 个答案:

答案 0 :(得分:1)

显示警报视图时会出错。使这项工作的方法是创建一个单独的功能。

- (void)noTwitterAccountMessage {
    UIAlertView *alertViewTwitter = [[UIAlertView alloc] initWithTitle:@"No Twitter Accounts"
                                                           message:@"There are no Twitter accounts configured. You can add or create a Twitter account in Settings."
                                                          delegate:nil
                                                 cancelButtonTitle:@"OK"
                                                 otherButtonTitles:nil];

    [alertViewTwitter show];
}

然后在帐户商店回调中,您应该像这样调用它

-(void)requestTwitterAccess
{
    [self.accStore requestAccessToAccountsWithType:self.twitterType
                     withCompletionHandler:^(BOOL granted, NSError *error) {
                         if (!granted) {
                             [self performSelectorOnMainThread:@selector(noTwitterAccountMessage) withObject:self waitUntilDone:NO];
                         }
}];

}