Skip to content

Instantly share code, notes, and snippets.

@jagbolanos
Created January 14, 2013 18:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jagbolanos/4532216 to your computer and use it in GitHub Desktop.
Save jagbolanos/4532216 to your computer and use it in GitHub Desktop.
- (void) postToTwitterPhotoAtIndexPath: (NSIndexPath*) indexPath {
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
PFObject *photo = [self.myPhotos objectAtIndex:indexPath.row];
// Create an account store object.
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
// Create an account type that ensures Twitter accounts are retrieved.
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
// Request access from the user to use their Twitter accounts.
[accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) {
if(granted) {
// Get the list of Twitter accounts.
NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];
// For the sake of brevity, we'll assume there is only one Twitter account present.
// You would ideally ask the user which account they want to tweet from, if there is more than one Twitter account present.
if ([accountsArray count] > 0) {
// Grab the initial Twitter account to tweet from.
ACAccount *twitterAccount = [accountsArray objectAtIndex:0];
// Create a request, which in this example, posts a tweet to the user's timeline.
// This example uses version 1 of the Twitter API.
// This may need to be changed to whichever version is currently appropriate.
NSString *publicText = [NSString stringWithFormat:@"Become immortal like me %@", [photo objectForKey:@"photo_url"]];
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
publicText, @"status",
nil];
TWRequest *postRequest = [[TWRequest alloc] initWithURL:[NSURL URLWithString:@"http://api.twitter.com/1/statuses/update.json"] parameters:params/*[NSDictionary dictionaryWithObject:publicText forKey:@"status"]*/ requestMethod:TWRequestMethodPOST];
// Set the account used to post the tweet.
[postRequest setAccount:twitterAccount];
// Perform the request created above and create a handler block to handle the response.
[postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
if (!error) {
[Util showAlert:@"Your Tweet has been sent!" withDelegate:nil];
} else {
[Util showAlert:@"There was a problem posting your picture to Twitter. Please try again" withDelegate:nil];
}
[MBProgressHUD hideHUDForView:self.view animated:YES];
}];
[postRequest release];
} else {
[Util showAlert:@"You don't seem to have Twitter configured on your phone" withDelegate:nil];
[MBProgressHUD hideHUDForView:self.view animated:YES];
}
} else {
NSLog(@"PERMISSION NOT GRANTED");
[MBProgressHUD hideHUDForView:self.view animated:YES];
}
}];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment