Compose Tweets

The TWTRComposer class presents a view to the user to compose Tweets. It provides methods to optionally configure the contents of the Tweet composition prior to presenting it.

../_images/compose_tweet.png

Present & Configure the Composer

You can set the initial content before presenting the composer to the user. Methods that set the content of a Tweet return a Boolean value; they return NO if the composer has already been presented. The completion handler has a single parameter which you can use to determine the result of the Tweet composition.

// Objective-C
TWTRComposer *composer = [[TWTRComposer alloc] init];

[composer setText:@"just setting up my Fabric"];
[composer setImage:[UIImage imageNamed:@"fabric"]];

// Called from a UIViewController
[composer showFromViewController:self completion:^(TWTRComposerResult result) {
    if (result == TWTRComposerResultCancelled) {
        NSLog(@"Tweet composition cancelled");
    }
    else {
        NSLog(@"Sending Tweet!");
    }
}];
// Swift
let composer = TWTRComposer()

composer.setText("just setting up my Fabric")
composer.setImage(UIImage(named: "fabric"))

// Called from a UIViewController
composer.showFromViewController(self) { result in
    if (result == TWTRComposerResult.Cancelled) {
        println("Tweet composition cancelled")
    }
    else {
        println("Sending tweet!")
    }
}

Note

Once a given instance of TWTRComposer has been shown, it cannot be reused; you must create a new instance of TWTRComposer every time you present it to the user.

Note

TWTRComposer relies on the existence of a local Twitter account from the Accounts.framework. If no account exists, attempting to show the composer will prompt the user to go to Settings and add a Twitter account.

Note

The TWTRComposer requires that you specify a view controller in which to present from. This has been updated from previous versions to fix a bug related to the composer being presented in the wrong view layer in certain situations.

Compose Tweets with App Cards

Requirements

Tweets with Cards have several requirements

  • The application must be live in the app store before Cards will be rendered
  • The Card image must be at least 800 pixels wide and 420 pixels tall, otherwise the Card won’t be rendered in the Twitter Android and iOS apps.
  • We recommend an aspect ratio of 1.91:1 to provide the best rendering. Other aspect ratios will be cropped.
  • Card must comply with App Card Composer Usage Guidelines.

Note

Your application must first be whitelisted by Twitter in order to post a Tweet with an App Card. Please review App Card Composer Usage Guidelines for requesting access.

If you’d like to attach an Application Card to the Tweet, create an instance of TWTRComposerViewController and supply a TWTRCardConfiguration which contains a user generated image and your application’s app ID. A valid application ID is required for an App Card to be rendered on iOS.

../_images/twitterkit_app_card_composer_example.png
// Objective-C
// Users must be logged-in to compose Tweets
TWTRSession *session = [Twitter sharedInstance].sessionStore.session;

// User generated image
UIImage *image = [[UIImage alloc] init];

// Create the card and composer
TWTRCardConfiguration *card = [TWTRCardConfiguration appCardConfigurationWithPromoImage:image iPhoneAppID:@"12345" iPadAppID:nil googlePlayAppID:nil];
TWTRComposerViewController *composer = [[TWTRComposerViewController alloc] initWithUserID:session.userID cardConfiguration:card];

// Optionally set yourself as the delegate
composer.delegate = self;

// Show the view controller
[self presentViewController:composer animated:YES completion:nil];
// Swift
// Users must be logged-in to compose Tweets
if let session = Twitter.sharedInstance().sessionStore.session() {

    // User generated image
    let image = UIImage()

    // Create the card and composer
    let card = TWTRCardConfiguration.appCardConfigurationWithPromoImage(image, iPhoneAppID: "12345", iPadAppID: nil, googlePlayAppID: nil)
    let composer = TWTRComposerViewController(userID: session.userID, cardConfiguration: card)

    // Optionally set yourself as the delegate
    composer.delegate = self

    // Show the view controller
    presentViewController(composer, animated: true, completion: nil)
}

Composer Delegate

Implement the TWTRComposerViewControllerDelegate to receive information about the Tweet operation. The view controller will automatically dismiss itself if either the user cancels the operation by tapping the cancel button or if the user successfully Tweets. If the Tweet fails for any reason the view controller will not dismiss giving the developer the opportunity to respond how they see fit.

Composer Theme

The TWTRComposerViewController supports both a light theme and a dark theme. To change the theme of the controller set the theme property to your desired TWTRComposerTheme before presenting the view controller.