When Apple introduced iOS 5.0, a convenient new feature was native Twitter support. Previously, it was the app’s responsibility to authorize the user and make the necessary REST API call to post a tweet, much like Corona’s “Twitter” sample app does.twitter-screen

With iOS 5.0, however, users could enter their Twitter credentials into their device settings and suddenly Twitter showed up on the Sharing screen along with Email, SMS, Print and so on. This made it very easy to manage Twitter. Near the end of last December, we provided access to this native Twitter feature.

Using “Native” Twitter

Corona provides access to the iOS built-in Twitter functionality using the native.showPopup() API call. This is the same API call that you can use to send email and SMS messages through the native interface. It’s also the same API that is used to access the native devices store and rating services.

The code is simple enough:

native.showPopup( "twitter", options )

Here, options is a Lua table with information needed for the tweet. The parameters are as follows:

  • image — a table of the form { baseDir=, filename= } to the image you wish to post.
  • message — a string that prepopulates the message.
  • listener — a listener that supports popup events.
  • url — a string for the URL you wish to post. Alternatively, this can be an array of URL strings.

In the most basic usage, you need to provide the message option:

local options = {
   message = "Hello Twitter world!"
}
native.showPopup( "twitter", options )

Of course, tweets  are limited to 140 characters. If you need to know when the tweet is complete and if the user committed to the tweet or cancelled it, you can define a call back handler to check on the user’s action.

local function tweetCallback( event )
   if ( event.action == "cancelled" ) then
      print( "User cancelled" )
   else
      print( "Thanks for the tweet!" )
   end
end

local options = {
   message = "Hello Twitter world!",
   listener = tweetCallback
}
native.showPopup( "twitter", options )

You can also include an image to be uploaded as well.

local function tweetCallback( event )
   if ( event.action == "cancelled" ) then
      print( "User cancelled" )
   else
      print( "Thanks for the tweet!" )
   end
end

local options = {
   message = "Hello Twitter world!",
   listener = tweetCallback,
   image = {
      baseDir = system.DocumentsDirectory,
      filename = "mypic.jpg"
      }
}
native.showPopup( "twitter", options )

Finally, you can include one or more URL(s) to post along with the tweet. The links will not show up in the popup screen, but they should appear when the tweet posts.

local function tweetCallback( event )
   if ( event.action == "cancelled" ) then
      print( "User cancelled" )
   else
      print( "Thanks for the tweet!" )
   end
end

local options = {
   message = "Hello Twitter world!",
   listener = tweetCallback,
   url = { "http://coronalabs.com", "http://apple.com" }
}
native.showPopup( "twitter", options )

Your app will show a popup screen that displays the tweet as it’s going to be sent. The user will also be given a chance to send or cancel it. This behavior is very similar to the way iOS handles emails or SMS messages from apps.

Please note this native functionality only works for iOS 5.0 and later, and it became available for your use in Corona in Daily Build 990. It is not supported on Android at this time.

  1. It seems that the “Cancel” and “Send” buttons of the popup are passing through the touch events to the storyboard below which is a problem in my app because at the same places I have “Back” and “Info” buttons which get triggered.

    Can someone confirm this ?

    • my fault, I use now system.ResourceDirectory and that works perfect:

      local options = {
      message = “Hello Twitter world, I have played xxx, very cool…”,
      listener = tweetCallback,
      url = { “http://coronalabs.com”},
      image = {
      baseDir = system.ResourceDirectory,
      filename = “pic/icon.png”
      }
      }
      print(“Hello Twitter…”)
      native.showPopup( “twitter”, options )

  2. Rob,

    When the popup is shown there’s always this error printed on the console
    Encountered an error while trying to respond to the roll of baseline…
    Error: Error Domain=NSCocoaErrorDomain Code=134310 “The operation couldn’t be completed. (Cocoa error 134310.)” …

    Sometimes the tweet is posted with no problems but occasionally it crashes

    (com.apple.twitterd[3519]) : (com.apple.twitterd) Job appears to have crashed: Segmentation fault: 11

    i’m using build 1030

  3. Unlike an older version, the result doesn’t show up in the simulator, at least as of build 1026. So if you’re a noob, don’t tear your hair out if you can’t see this on your desktop, just view it on a device. :-)

  4. Works perfect! Now if we can only get support for the iOS “twitter keyboard”!

    Hope to see the built in support for posting images to Facebook soon too.

    Thanks Corona team!

  5. Just found this!

    I was wondering what build I need? (would #992 works?) and more importantly is this would work with device like the 3GS who has OS 4.3? I am pretty sure not many of my users have OS 4.3 so I was wondering if Apple would reject my app update with this built-in Twitter code IF it will not work on 4.3?

    Thanks for this!

    Mo

  6. I went through the tutorial which is pretty easy to understand, but when I go ahead and post on twitter there is a single tweet, but the image is uploaded over and over again until the app is closed. Meaning there are many of the same images being uploaded to twitter. :-/ Tweet works but the image posting is causing problems.

  7. is there a way to make it so the user cant edit the post??
    I want to use this to post a score to twitter but with this its possible to edit whats being posted to twitter… is there any way of stopping this??

    • Not with this tool. The idea that Apple wants is for the user to have the ability to edit it. That is the feature of the sharing button in IOS.

      If you want more control, I would suggest you look at the Twitter sample app, where you can access the Twitter REST API directly and auto post things without user’s having a chance to edit them.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>