How to send an image with a private tweet using SLRequest in iOS?
I have a problem that I have some followers and I want to send a direct message with an image to all the followers, but I didn't have a way to do this.
I have been going through all the documentation but cannot find a way to do this.
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've seen all other similar questions / answers on stackoverflow, but I haven't had a way to send a direct image message to followers.
Please do not treat this question as a duplicate and try to solve my problem. I will be grateful for your every hint.
+3
source to share
1 answer
Here is my code to accomplish this task, it works for me .. hopefully useful for you as well ..
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"];
+6
source to share