I want to post a photo to twitter from my iOS app. I can post a tweet without media but when i am trying to attach media it throws an error.
I am following twitter documentation and according to that first I need to upload media to https://upload.twitter.com/1.1/media/upload.json and then I will be able to attach it with the tweet using media-id.
here is my code for uploading media.
App is crashing at URLRequestWithMedthod call.
Help me to resolve issue.
UIImage *image = [UIImage imageNamed:@"shareit.png"];
NSData *imageData = UIImageJPEGRepresentation(image, 0.7);
NSString *statusesShowEndpoint = @"https://upload.twitter.com/1.1/media/upload.json";
NSDictionary *params = @{@"media" : imageData};
NSError *clientError;
NSURLRequest *request = [[[Twitter sharedInstance] APIClient]
URLRequestWithMethod:@"POST"
URL:statusesShowEndpoint
parameters:params
error:&clientError];
if (request) {
[[[Twitter sharedInstance] APIClient]
sendTwitterRequest:request
completion:^(NSURLResponse *response,
NSData *data,
NSError *connectionError) {
if (data) {
// handle the response data e.g.
NSError *jsonError;
NSDictionary *json = [NSJSONSerialization
JSONObjectWithData:data
options:0
error:&jsonError];
NSLog(@"%@",json);
}
else {
NSLog(@"Error: %@", connectionError);
}
}];
}
else {
NSLog(@"Error: %@", clientError);
}
Well it was pretty simple. All was missing is conversion of imagedata into base64EncodedString. Here is the solution.
Swift 4 latest
install pod file pod 'TwitterKit'
https://developer.twitter.com/en/docs/media/upload-media/api-reference/post-media-upload-init.html
Completing the Saani's answer
Sharing the swift 2.2 (Xcoe 7.3.2) code if anyone is still looking for help.
To Send with status update.