-
Notifications
You must be signed in to change notification settings - Fork 448
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. There is also an option of TWTRComposerViewController
which replaces App Card composer and supports video uploading, along with
delegate
property to be notified of life-cycle changes. However, TWTRComposerViewController
requires that developers handle the login process themselves if there are no logged in sessions.
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 Twitter Kit"];
[composer setImage:[UIImage imageNamed:@"twitterkit"]];
// 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 Twitter Kit")
composer.setImage(UIImage(named: "twitterkit"))
// Called from a UIViewController
composer.show(from: self.navigationController!) { (result in
if (result == .done) {
print("Successfully composed Tweet")
} else {
print("Cancelled composing")
}
}
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 in current session. If no account exists, attempting to show the composer will prompt the user to log in to Twitter app or on a web view.
Alternatively, (especially for those looking for previous App Card composer functionality) you can also use TWTRComposerViewController
to present a composer. All you need is to create an instance of TWTRComposerViewController
and present it just like any other UIViewController
. At least one logged-in user in the app is required to proceed.
The view controller will automatically handle dismissal if the user either successfully Tweets or cancels the operation by tapping the cancel button.
NOTE: Make sure to check for current logged in session before calling TWTRComposerViewController
. Follow below example if you are not familiar with set up.
// 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 (TWTRTwitter.sharedInstance().sessionStore.hasLoggedInUsers()) {
// App must have at least one logged-in user to compose a Tweet
let composer = TWTRComposerViewController.emptyComposer()
present(composer, animated: true, completion: nil)
} else {
// Log in, and then check again
TWTRTwitter.sharedInstance().logIn { session, error in
if session != nil { // Log in succeeded
let composer = TWTRComposerViewController.emptyComposer()
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)
}
}
}
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
.
NOTE: When using NSData for video upload, you must provide preview image that goes with the video. Or else, it will return nil.
// 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)
}
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 ofassets-library
. Use keyUIImagePickerControllerMediaURL
from thedidFinishPickingMediaWithInfo:
info parameter.
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.