4

我正在使用以下代码来访问用户的 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...]吗?每次?

4

2 回答 2

8

您可以保存TWAccount.identifier,然后用于[ACAccountStore accountWithIdentifier]稍后检索相同的帐户。

于 2012-08-09T20:11:13.827 回答
-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];
}

}
于 2012-05-27T21:56:34.113 回答