【问题标题】:TweetSheet is awesome,but can I automate a tweet while using it?TweetSheet 很棒,但我可以在使用它的同时自动生成一条推文吗?
【发布时间】:2012-06-15 17:57:37
【问题描述】:

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

有没有办法解决这个问题?本质上,我希望我的推文在我点击推文按钮的那一刻被推文,我不想再次点击推文表上的发送按钮?可能吗?

编辑: 根据下面答案中发布的链接,我浏览了一些 twitter API 示例,并在此处找到此代码https://dev.twitter.com/docs/ios/posting-images-using-twrequest。它工作正常,但当应用程序到达由我说应用程序崩溃的评论标记的部分时崩溃。一个错误 'NSInvalidARgumentException''data parameter' 是 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...
          });
    }];
}****

【问题讨论】:

    标签: xcode ios5 twitter tweets


    【解决方案1】:

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

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

    您还需要阅读 twitter API 文档。

    编辑:

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

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

    【讨论】:

    • phix23 嘿,感谢您的链接,我根据在链接中找到的信息对问题进行了一些更改,非常感谢您的帮助。谢谢
    • 这解决了我的问题,并且该应用程序运行良好,但是当我在一段时间后第二次启动它时(我总是使用模拟器进行测试)它不会发布到 Twitter,给出我一个错误:错误=“无法验证你。” request="/1/statuses/update_with_media.json";
    • requestAccessToAccountsWithType 是异步的。在开始 twitter 请求之前,请确保您确实拥有一个有效的帐户对象。
    • 感谢您的所有帮助,作为一个初学者非常感谢它,我重新检查了我的代码以确保我有一个有效的 accout 对象,但现在它又向我抛出了另一个错误,Invalid unicode value in一个或多个参数!我检查以确保并发现图像数据和文本数据是由分配的变量获取的。现在不知道为什么会出现此错误:(
    猜你喜欢
    • 2018-09-06
    • 2017-10-15
    • 2017-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多