Created
January 28, 2015 23:50
-
-
Save shepting/11457741f35465a1f952 to your computer and use it in GitHub Desktop.
Upload an image using Twitter Fabric
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[[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