Apple’s Twitter framework example

This is an example, how to integrate Twitter framework of Apple to your app.

Firstly, add Twitter (Twitter.framework) and MobileCoreServices frameworks to your app.

Then add header files to your source file:

[crayon-662129a2a2d8b626978346/]

Then add this method or similar:

- (void)sendToTwitter {
useDelegate
if ([appDelegate iOSVersion5]) {
[self showErrorAlert:@"Отправка в Twitter доступна только в iOS 5 и выше."];
return;
}
tweetView = [[TWTweetComposeViewController alloc] init];
TWTweetComposeViewControllerCompletionHandler
completionHandler =
^(TWTweetComposeViewControllerResult result) {
switch (result)
{
case TWTweetComposeViewControllerResultCancelled: {
NSLog(@"Twitter Result: canceled");
[self showErrorAlert:@"Отправка в Twitter отменена."];
break;
}
case TWTweetComposeViewControllerResultDone: {
NSLog(@"Twitter Result: sent");
[self showErrorAlert:@"Отправка в Twitter выполнена."];
break;
}
default: {
NSLog(@"Twitter Result: default");
[self showErrorAlert:@"Отправка в Twitter не выполнена."];
break;
}
}
[self dismissModalViewControllerAnimated:YES];
};
[tweetView setCompletionHandler:completionHandler];
[tweetView setInitialText:self.textToExport];
[self presentModalViewController:tweetView animated:YES];
}

Here, firstly iOS version is checked, because it will work only in iOS 5 or later. Then, I create TWTweetComposeViewController instance, it’s block completion handler, set text to export to Twitter and then present controller as a modal controller.

How to check iOS version programmatically

To check the iOS version you can use the following method: