2012-02-29 32 views
2

我正在使用iOS5 Twitter API獲得用戶使用其Twitter帳戶的權限。這將彈出一個對話框,用戶可以從中選擇對應用程序授予或拒絕權限。如果用戶接受但是沒有在iPhone上設置Twitter帳戶,我希望能夠打開警報,但由於已經打開對話框,此時打開警報失敗。如何在Twitter權限對話解除後立即添加警報?在iOS5中使用提醒進行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 
     } 

     } 
    }]; 
} 
+0

什麼叫「在這一點上打開一個警報失敗」意味着什麼。應用程序崩潰了嗎?或者根本沒有警報?如果應用程序沒有崩潰,並且看不到警報,您確定警報不存在嗎? – GnarlyDog 2012-05-29 15:14:14

回答

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]; 
         } 
}]; 

}