Skip to content

Instantly share code, notes, and snippets.

@shepting
Created January 28, 2015 23:50
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shepting/11457741f35465a1f952 to your computer and use it in GitHub Desktop.
Save shepting/11457741f35465a1f952 to your computer and use it in GitHub Desktop.
Upload an image using Twitter Fabric
[[Twitter sharedInstance] logInWithCompletion:^(TWTRSession *session, NSError *error){
// Ensure the auth worked
if (session) {
NSLog(@"Twitter Session %@ / %@ found", session.authToken, session.authTokenSecret);
} else {
NSLog(@"Login error: %@", error);
}
// Get a random image
UIImage *corgiImage = [UIImage imageNamed:@"Corgis_Small.jpeg"];
NSData *corgiData = UIImageJPEGRepresentation(corgiImage, 0.9);
NSString *corgiString = [corgiData base64EncodedStringWithOptions:0];
NSString *mediaEndpoint = @"https://upload.twitter.com/1.1/media/upload.json";
NSError *requestError;
TWTRAPIClient *client = [[Twitter sharedInstance] APIClient];
// Build the POST request with the image data in the body
NSURLRequest *request = [client URLRequestWithMethod:@"POST" URL:mediaEndpoint parameters:@{@"media":corgiString} error:&requestError];
if (request) {
[[[Twitter sharedInstance] APIClient] sendTwitterRequest:request completion:^(NSURLResponse *urlResponse, NSData *data, NSError *connectionError) {
if (data) {
NSError *parsingError;
// Grab the JSON response
NSDictionary *responseJSON = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&parsingError];
if (!responseJSON) {
NSLog(@"Parsing error: %@", parsingError);
} else {
// Valid JSON here
NSLog(@"Response: %@", responseJSON);
}
} else {
NSLog(@"Connection error: %@", connectionError);
}
}];
} else {
NSLog(@"Request generation error: %@", requestError);
}
}];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment