简体   繁体   中英

How to post image with private tweet using SLRequest in iOS?

I have a problem that I have some followers and I want to send a direct message with image to all followers but I didn't get a way to do that.

I study whole documentation but can't find a way to do that.

My code is here:

NSString   *strUrl = @"https://api.twitter.com/1.1/direct_messages/new.json";
NSURL      *url    = [NSURL URLWithString:strUrl];
strUrl = nil;

NSMutableDictionary    *parameters = [NSMutableDictionary new];
[parameters setValue:@"name" forKey:@"screen_name"];
[parameters setValue:@"123456" forKey:@"user_id"];
[parameters setValue:@"sending text" forKey:@"text"];

// Creating a request.
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter
                                                 requestMethod:SLRequestMethodPOST
                                                           URL:url
                                                    parameters:parameters];

NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.greenuplawnandsprinklers.com/uploads/design_sample_landscape.jpg"]];
[request addMultipartData:imageData
                 withName:@"media[]"
                     type:@"image/jpeg"
                 filename:@"image.jpg"];
imageData = nil;

[request setAccount:twitAccount];
url        = nil;
parameters = nil;

// Perform the request.
[request performRequestWithHandler:^(NSData *responseData,NSHTTPURLResponse *urlResponse,NSError *error)
{
dispatch_async(dispatch_get_main_queue(), ^{
      // Check if we reached the rate limit.
      if ([urlResponse statusCode] == 429)
      {
        NSLog(@"Rate limit reached");
        return;
      }

if(LOGS_ON) NSLog(@"TwitterFriendModel-->shareMagazineTitle-->error = %@",error);

// Check if there is some response data.
if (responseData)
{
   NSError *error = nil;
   NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&error];

NSLog(@"dictionary = %@",dictionary);
dictionary = nil;
}
});
}];
}

I saw all other similar question/answers on stackoverflow but I didn't get a way to send direct message with image to followers.

Please don't consider this question as duplicate and try to resolve my problem. I will appreciate your every clue.

Here is my code to accomplish this task, it works for me.. hope also helpfull for you..

 NSDictionary *message = @{@"status": messagetext};
                 NSURL *requestURL = [NSURL
                                      URLWithString:@"https://upload.twitter.com/1/statuses/update_with_media.json"];
                 SLRequest *postRequest = [SLRequest
                                           requestForServiceType:SLServiceTypeTwitter
                                           requestMethod:SLRequestMethodPOST
                                           URL:requestURL parameters:message];
                 UIImage *image = [self imageReduceSize:[UIScreen mainScreen].bounds.size:imgPost.image];
                 NSData *myData = UIImagePNGRepresentation(image);
                 [postRequest addMultipartData:myData withName:@"media" type:@"image/png" filename:@"TestImage"];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM