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

后端 未结 4 1712
面向向阳花
面向向阳花 2021-02-06 12:52

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 docu

4条回答
  •  太阳男子
    2021-02-06 13:14

    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
        }];
    

提交回复
热议问题