Snowman
Snowman

Reputation: 32071

Saving a twitter account in iOS 5?

I'm using the following code to get access to a user's twitter account:

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

My question is, how do I save the user's account in between app sessions? For the current session I can store it in an instance variable, but if the user exits the app and comes back, how do I get that account? Do I have call [store requestAccessToAccountsWithType:twitterAccountType...]? every time?

Upvotes: 4

Views: 1395

Answers (2)

HoratioCain
HoratioCain

Reputation: 915

You can save the TWAccount.identifier, and then use [ACAccountStore accountWithIdentifier] to retrieve that same account later.

Upvotes: 8

Max Roper
Max Roper

Reputation: 11

If you are just concerned with iOS 5, you can use the TWTweetComposeViewController class.

The way I have it working is like this...

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

}

Upvotes: -4

Related Questions