Skip to content

Instantly share code, notes, and snippets.

@melsam
Last active October 24, 2016 12:05
Show Gist options
  • Save melsam/85593681dbf8c60a5e54 to your computer and use it in GitHub Desktop.
Save melsam/85593681dbf8c60a5e54 to your computer and use it in GitHub Desktop.
Post a Twitter status update without using any of the built-in UI on iOS. This uses the Twitter 1.1 API (not the deprecated REST APIs).
@import Accounts;
#import <Twitter/Twitter.h>
- (void)postTwitterStatusUpdate:(NSString *)statusMessage
{
ACAccountStore *account = [ACAccountStore new];
ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[account requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error) {
if (granted) {
// Populate an array with all available Twitter accounts.
NSArray *arrayOfAccounts = [account accountsWithAccountType:accountType];
if ([arrayOfAccounts count] == 0) {
// There are no Twitter accounts configured. Display an appropriate error message.
} else {
NSDictionary *message = @{@"status": statusMessage};
NSURL *requestURL = [NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/update.json"];
SLRequest *postRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter
requestMethod:SLRequestMethodPOST
URL:requestURL
parameters:message];
postRequest.account = [arrayOfAccounts firstObject];
[postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
if (error) {
// There was an error performing the request.
NSLog(@"%@", error.description);
}
else {
NSLog(@"Successfully posted status update!");
}
}];
}
}
}];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment