Compose Tweets

The TWTRComposerViewController is the preferred interface to allow users to compose Tweets inside your app. It provides functionality to compose Tweets with optional image, video, and text attachments. It also takes and optional delegate property to be notified of life-cycle changes.

../../_images/compose_tweet.png

Presenting a Basic Composer

To present a composer, all you need is to create an instance of TWTRComposerViewController and present it just like any other UIViewController. All that is required is for at least one logged-in user in the app.

Th view controller will automatically handle dismissal if the user either successfully Tweets or cancels the operation by tapping the cancel button.

// Check if current session has users logged in
if ([[Twitter sharedInstance].sessionStore hasLoggedInUsers]) {
    TWTRComposerViewController *composer = [TWTRComposerViewController emptyComposer];
    [fromController presentViewController:composer animated:YES completion:nil];
} else {
    [[Twitter sharedInstance] logInWithCompletion:^(TWTRSession *session, NSError *error) {
        if (session) {
            TWTRComposerViewController *composer = [TWTRComposerViewController emptyComposer];
            [fromController presentViewController:composer animated:YES completion:nil];
        } else {
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"No Twitter Accounts Available" message:@"You must log in before presenting a composer." preferredStyle:UIAlertControllerStyleAlert];
            [self presentViewController:alert animated:YES completion:nil];
        }
    }];
}
// Swift

if (Twitter.sharedInstance().sessionStore.hasLoggedInUsers()) {
    // App must have at least one logged-in user to compose a Tweet
    let composer = TWTRComposerViewController.emptyComposer()
    composer.delegate = self
    present(composer, animated: true, completion: nil)
} else {
    // Log in, and then check again
    Twitter.sharedInstance().logIn { session, error in
        if session != nil { // Log in succeeded
            let composer = TWTRComposerViewController.emptyComposer()
            composer.delegate = self
            self.present(composer, animated: true, completion: nil)
        } else {
            let alert = UIAlertController(title: "No Twitter Accounts Available", message: "You must log in before presenting a composer.", preferredStyle: .alert)
            self.present(alert, animated: false, completion: nil)
        }
    }
}

Providing Default Attachments

To add default text, image, or video attachments to a Tweet you can provide those values when creating the TWTRComposerViewController.

When providing default text for a Tweet, you may add user mentions at the beginning of initialText and links at the *end*.

Note

Only one attachment type may be provided, either UIImage or videoURL.

// MARK: Image Picker with TWTRComposerViewController

func presentImagePicker() {
    let picker = UIImagePickerController()
    picker.delegate = self
    picker.mediaTypes = [String(kUTTypeImage), String(kUTTypeMovie)]
    present(picker, animated: true, completion: nil)
}

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
    dismiss(animated: true)
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    // Dismiss the image picker
    dismiss(animated: true, completion: nil)

    // Grab the relevant data from the image picker info dictionary
    let image = info[UIImagePickerControllerOriginalImage] as? UIImage
    let fileURL = info[UIImagePickerControllerMediaURL] as? URL

    // Create the composer
    let composer = TWTRComposerViewController(initialText: "Check out this great image: ", image: image, videoURL:fileURL)
    composer.delegate = self
    present(composer, animated: true, completion: nil)
}

Composing Tweets with Video

Requirements

Tweets with video currently have several limitations. Video files must meet all of the following criteria:

  • Duration should be between 0.5 seconds and 30 seconds
  • File size should not exceed 5 mb
  • Dimensions should be between 32x32 and 1280x1024
  • Aspect ratio should be between 1:3 and 3:1
  • Frame rate should be 40fps or less
  • Video must comply with REST API Video Recommendation.
  • The videoURL parameter is required to have format of assets-library. Use key UIImagePickerControllerMediaURL from the didFinishPickingMediaWithInfo: info parameter.

Composer Delegate

Implement the TWTRComposerViewControllerDelegate methods and set the delegate property to receive lifecycle updates about the Tweet compose operation.

If the user successfully sends a Tweet, the composerDidSucceed:withTweet: method will be called with the newly created TWTRTweet object that has been sent.

If sending the Tweet fails, the composerDidFail:withError: method will be called with an NSError object describing the failure.

If the user cancels the compose operation, the composerDidCancel: method will be called.

TWTRComposer Deprecation

This class has been deprecated in Twitter Kit 3.0. * it only provides TWTRComposerResultCancelled and TWTRComposerResultDone states in the TWTRComposerCompletion block. * it provides less flexibility in presenting since it doesn’t return a UIViewController but instead handles all of the presentation logic internally.

Note

The TWTRComposer handles automatically presenting a login view controller to user if they have not yet logged in.