Take the 2-minute tour ×

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'

    NSURL *url = 
    [NSURL URLWithString:

  //  Create a POST request for the target endpoint
    TWRequest *request = 
    [[TWRequest alloc] initWithURL:url 

  //  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 

  // 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] 

  //  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:
      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...
share|improve this question
I edited my answer –  phix23 Jun 16 '12 at 15:20
add comment

1 Answer

up vote 1 down vote accepted

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: http://stackoverflow.com/a/9751878/550177

You will also need to read the twitter API documentation.


  • 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:).

share|improve this answer
phix23 Hey, thanks for the link, I made some changes to the question based on the info I found at the link, would appreciate your help.Thanks –  OccamsRazor Jun 16 '12 at 15:05
That solved my issue, and the app was working perfectly, but when I fired it up the second time after a while now (I use the simulator to test always) it does not post to twitter, gives me an error: error="could not authenticate you." request="/1/statuses/update_with_media.json"; –  OccamsRazor Jun 17 '12 at 13:22
requestAccessToAccountsWithType is asynchronous. Make sure you really have a valid account object before starting the twitter request. –  phix23 Jun 17 '12 at 13:25
Thanks for all your help, as a beginner really appreciate it, I rechecked my code to make sure I had a valid accout object, but now this it is throwing another error at me, Invalid unicode value in one or more parameters! I checked to make sure and found that the image data and text data is taken by the assigned variables.No idea why this error now :( –  OccamsRazor Jun 17 '12 at 15:00
add comment

Your Answer


