如何使用iOS中的SLRequest使用私人推文发布图像?

时间:2014-08-13 12:19:40

标签: ios post twitter slrequest

我有一个问题,我有一些粉丝,我想向所有粉丝发送带有图片的直接信息,但我没有办法做到这一点。

我研究整个文档,但无法找到方法。

我的代码在这里:

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

我在stackoverflow上看到了所有其他类似的问题/答案,但我没有办法向关注者发送带图像的直接消息。

请不要将此问题视为重复,并尝试解决我的问题。我将感激你的每一个线索。

1 个答案:

答案 0 :(得分:6)

这是我完成此任务的代码,它对我有用..希望对你有帮助..

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