Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - Unable to Upload media with Twitter/Fabric New SDK

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);
}
like image 593
Hussaan S Avatar asked Apr 10 '15 07:04

Hussaan S


2 Answers

Well it was pretty simple. All was missing is conversion of imagedata into base64EncodedString. Here is the solution.

   NSString *media = @"https://upload.twitter.com/1.1/media/upload.json";

   NSData *imageData = UIImageJPEGRepresentation(image, 0.9);

   NSString *imageString = [imageData base64EncodedStringWithOptions:0];
   NSError *error;
   NSURLRequest *request = [[[Twitter sharedInstance] APIClient] URLRequestWithMethod:@"POST" URL:media parameters:@{@"media":imageString} error:&error];

   [[[Twitter sharedInstance] APIClient] sendTwitterRequest:request completion:^(NSURLResponse *urlResponse, NSData *data, NSError *connectionError) {

       NSError *jsonError;
       NSDictionary *json = [NSJSONSerialization
                              JSONObjectWithData:data
                              options:0
                              error:&jsonError];
       NSLog(@"Media ID :  %@",[json objectForKey:@"media_id_string"]);

      // Post tweet With media_id
    }];
like image 84
Hussaan S Avatar answered Nov 12 '22 03:11

Hussaan S


Completing the Saani's answer

// Post tweet With media_id

mediaID = [json objectForKey:@"media_id_string"];
client = [[Twitter sharedInstance] APIClient];
message = @{@"status": title, @"wrap_links": @"true", @"media_ids": mediaID};

NSURLRequest *request = [client URLRequestWithMethod:@"POST" URL:@"https://api.twitter.com/1.1/statuses/update.json" parameters:message error:&error];

[client sendTwitterRequest:request completion:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {

    if (connectionError) {
        NSLog(@"error %@",[connectionError localizedDescription]);
    } else {
        NSError *jsonError;
        NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];
        NSLog(@"json Finish! %@",json);
    }
}];
like image 34
jose920405 Avatar answered Nov 12 '22 05:11

jose920405