OccamsRazor
OccamsRazor

Reputation: 79

TweetSheet is awesome,but can I automate a tweet while using it?

I have integrated twitter with my iOS 5 app, the tweet sheet as it is called in short, works fine, but it brings up the twitter interface whenever I hit the tweet button, I managed to get rid of that by not including the self presentmodalviewcontroller in the code, but I can't find a way to automate the tweet, because unless I hit the send button on the tweetsheet the tweet is not tweeted.

Is there a way around this? In essence I want my tweet to be tweeted the moment I hit the tweet button, I do not want to hit the send button again on the tweet sheet? Possible?

EDIT: As per the link posted in the answer below I went through some of the twitter API examples and found this code here https://dev.twitter.com/docs/ios/posting-images-using-twrequest.It works fine but crashes when the app reaches part marked by a comment by me saying the app crashes.,with an error 'NSInvalidARgumentException''data parameter' is nil and a bunch of numbers.

Also I can't set the account using this line [request setAccount:[self.accounts objectAtIndex:0]]; it gives me an error property accounts not found on object of type '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...
          });
    }];
}****

Upvotes: 0

Views: 1364

Answers (1)

Felix
Felix

Reputation: 35394

Yes that's possible using a lower level API, namely the class TWRequest. With this you can do all kinds of twitter requests without presenting a UI. You can also provide your own user interface for tweeting.

See my other answer here too see sample code on how to use it: https://stackoverflow.com/a/9751878/550177

You will also need to read the twitter API documentation.

EDIT:

  • In the POST request make sure that the image exists which you append to the request. In your sample code a file named 'larry.png' must be in the app bundle. Check imageData - it must not be nil!

  • in order to make Twitter's example code work you need to save all ACAccount's in a property named accounts (an NSArray of all accounts you got from requestAccessToAccountsWithType:).

Upvotes: 1

Related Questions