2012-05-27 22 views
4

我使用下面的代碼可以訪問用戶的Twitter帳戶:在iOS 5中保存一個Twitter賬號?

ACAccountStore *store = [[ACAccountStore alloc] init]; 
ACAccountType *twitterAccountType = 
[store accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter]; 

// Request access from the user for access to his Twitter accounts 
[store requestAccessToAccountsWithType:twitterAccountType 
       withCompletionHandler:^(BOOL granted, NSError *error) { 
        if (!granted) { 
         NSLog(@"User rejected access to his account."); 
        } 
        else { 
         NSArray *arrayOfAccounts = [store accountsWithAccountType:twitterAccountType]; 
         if ([arrayOfAccounts count] > 0) { 
          ACAccount *acct = [arrayOfAccounts objectAtIndex:0]; 
         } 
}]; 

我的問題是,我該如何保存用戶的帳戶中的應用程序會話之間?對於當前會話,我可以將它存儲在實例變量中,但如果用戶退出應用程序並返回,我如何獲取該帳戶?我打電話給[store requestAccessToAccountsWithType:twitterAccountType...]嗎?每次?

回答

8

可以保存TWAccount.identifier,然後用[ACAccountStore accountWithIdentifier]以後檢索同一帳戶。

-4

如果您只關心iOS 5,則可以使用TWTweetComposeViewController類。

我有它的工作方式是這樣的...

NSString *deviceType = [UIDevice currentDevice].systemVersion; 
NSString *tweet; 
tweet=[API tweet:appDelegate.stadium_id]; 

if([deviceType hasPrefix:@"5."]){ 

    // Create the view controller 
    TWTweetComposeViewController *twitter = [[TWTweetComposeViewController alloc] init]; 

    [twitter setInitialText:tweet]; 

    // Show the controller 
    [self presentModalViewController:twitter animated:YES]; 

    // Called when the tweet dialog has been closed 
    twitter.completionHandler = ^(TWTweetComposeViewControllerResult result) 
    { 
     NSString *title = @"Tweet Status"; 
     NSString *msg; 

     if (result == TWTweetComposeViewControllerResultCancelled) 
      msg = @"Tweet was canceled."; 
     else if (result == TWTweetComposeViewControllerResultDone) 
      msg = @"Tweet is sent!"; 

     // Show alert to see how things went... 
     UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:nil]; 
     [alertView show]; 
     // Dismiss the controller 
     [self dismissModalViewControllerAnimated:YES]; 

    };   
} 

else{ 

    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Alert" message:@"Please upgrade to iOS 5 to tweet." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [alert show]; 
} 

} 
+0

好吧,我會在後臺發佈推文,而無需作曲者視圖。 – Snowman

+0

只是好奇,爲什麼這個答案被接受?它似乎沒有回答你的問題。 – HoratioCain