TweetSheet很棒,但我可以在使用时自动发送推文吗?

时间:2012-06-15 17:57:37

标签: xcode ios5 twitter tweets

我已将twitter与我的iOS 5应用程序集成,因为它简称为推文,工作正常,但每当我点击推文按钮时它会调出twitter界面,我设法通过不包括代码中的self presentmodalviewcontroller,但我找不到自动发推文的方法,因为除非我点击推文上的发送按钮,否则推文不会发推文。

有解决方法吗?从本质上讲,我希望在我点击推文按钮的时候发送推文,我不想在推文表上再次点击发送按钮?可能的?

编辑: 根据下面答案中发布的链接,我浏览了一些Twitter API示例,并在此处找到了此代码https://dev.twitter.com/docs/ios/posting-images-using-twrequest。它工作正常,但当应用程序到达标记为部分标记应用程序崩溃的部分时崩溃。 ,错误'NSInvalidARgumentException''data参数'是nil和一堆数字。

此外,我无法使用此行[request setAccount:[self.accounts objectAtIndex:0]];设置帐户 它给了我一个错误属性帐户找不到类型'myviewcontrollername'

的对象
(void)performTWRequestUpload
    {
    NSURL *url = 
    [NSURL URLWithString:
      @"https://upload.twitter.com/1/statuses/update_with_media.json"];

  //  Create a POST request for the target endpoint
    TWRequest *request = 
    [[TWRequest alloc] initWithURL:url 
                        parameters:nil 
                     requestMethod:TWRequestMethodPOST];

  //  self.accounts is an array of all available accounts; 
  //  we use the first one for simplicity
     [request setAccount:[self.accounts objectAtIndex:0]];

  //  The "larry.png" is an image that we have locally
  UIImage *image = [UIImage imageNamed:@"larry.png"];

  //  Obtain NSData from the UIImage 
  NSData *imageData = UIImagePNGRepresentation(image);

  //  Add the data of the image with the 
  //  correct parameter name, "media[]"
  [request addMultiPartData:imageData 
                   withName:@"media[]" 
                       type:@"multipart/form-data"];

  // NB: Our status must be passed as part of the multipart form data
  NSString *status = @"just setting up my twttr #iOS5";

  //  Add the data of the status as parameter "status"
  [request addMultiPartData:[status dataUsingEncoding:NSUTF8StringEncoding] 
                   withName:@"status" 
                       type:@"multipart/form-data"];

  //  Perform the request.  
  //    Note that -[performRequestWithHandler] may be called on any thread,
  //    so you should explicitly dispatch any UI operations to the main thread 

//Problem code part upon reaching which app crashes
[request performRequestWithHandler:
    ^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
      NSDictionary *dict = 
        (NSDictionary *)[NSJSONSerialization 
          JSONObjectWithData:responseData options:0 error:nil];

      // Log the result
      NSLog(@"%@", dict);

      dispatch_async(dispatch_get_main_queue(), ^{
          // perform an action that updates the UI...
          });
    }];
}****

1 个答案:

答案 0 :(得分:1)

是的,可以使用较低级别的API,即类TWRequest。 有了这个,您可以在不显示UI的情况下执行各种Twitter请求。您还可以提供自己的用户界面以进行推文。

在此处查看我的其他答案,请参阅有关如何使用它的示例代码: https://stackoverflow.com/a/9751878/550177

您还需要阅读twitter API文档。

编辑:

  • 在POST请求中,确保存在您附加到请求的图像。在示例代码中,名为“larry.png”的文件必须位于应用程序包中。检查imageData - 一定不能为零!

  • 为了使Twitter的示例代码工作,您需要将所有ACAccount保存在名为accounts的属性中(您从requestAccessToAccountsWithType:获得的所有帐户的NSArray)