Twitter in iPhone


In this article I will create a Single View application. Here I use an image view and two buttons from outlet. In this app I will implement uploading an image to Twitter. For this we implement an image gallery path from which we pick an image and upload it to Twitter. Finally we test this app in a device. It was successfully uploaded to Twitter. 

To understand it we use the following.

Step 1

Here first we add framework Twitter which is required to implement the Twitter class.

To import this framework we use the following.

Step 2

Click on the project and select Build Phase.


Step 3

Click on the "+" icon to add the framework.


Step 4

Now select the Twitter framework and click on the add button.


Step 5

But TWTweetComposeViewController is deprecated from iOS 6; now in iOS 6 we use SLComposeViewController instead of TWTweetComposeViewController.

So here we add the Social framework framework which is required to implement the Twitter class.


Step 6

To pick an image from the image gallery we need one more framework; MobileCoreService.


Step 7

Now we write the code for each class.


#import <UIKit/UIKit.h>

@class twitterappViewController;

@interface twitterappAppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) twitterappViewController *viewController;



#import "twitterappAppDelegate.h"

#import "twitterappViewController.h"

@implementation twitterappAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[twitterappViewController alloc] initWithNibName:@"twitterappViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;

- (void)applicationWillResignActive:(UIApplication *)application
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

- (void)applicationDidEnterBackground:(UIApplication *)application
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

- (void)applicationWillEnterForeground:(UIApplication *)application
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.

- (void)applicationDidBecomeActive:(UIApplication *)application
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

- (void)applicationWillTerminate:(UIApplication *)application
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.



#import <UIKit/UIKit.h>
#import <Twitter/Twitter.h>
#import <MobileCoreServices/MobileCoreServices.h>

@interface twitterappViewController : UIViewController
<UIImagePickerControllerDelegate, UINavigationControllerDelegate>

@property (strong, nonatomic) IBOutlet UIImageView *imageView;
@property (strong, nonatomic) SLComposeViewController *tweetView;




#import "twitterappViewController.h"

@implementation twitterappViewController
@synthesize tweetView, imageView;

- (void) previewTweet

[tweetView addImage:imageView.image];

[self presentModalViewController:tweetView animated:YES];

- (void) selectImage
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerController *imagePicker =
[[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType =
imagePicker.mediaTypes = [NSArray arrayWithObjects:
(NSString *) kUTTypeImage,
imagePicker.allowsEditing = NO;
[self presentModalViewController:imagePicker animated:YES];


-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
NSString *mediaType = [info
[self dismissModalViewControllerAnimated:YES];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
UIImage *image = [info

imageView.image = image;

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
[self dismissModalViewControllerAnimated:YES];

- (void)didReceiveMemoryWarning
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.

#pragma mark - View lifecycle

- (void)viewDidLoad
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

tweetView = [[TWTweetComposeViewController alloc] init];

TWTweetComposeViewControllerCompletionHandler completionHandler =
^(TWTweetComposeViewControllerResult result) {
switch (result)
case TWTweetComposeViewControllerResultCancelled:
NSLog(@"Twitter Result: canceled");
case TWTweetComposeViewControllerResultDone:
NSLog(@"Twitter Result: sent");
NSLog(@"Twitter Result: default");
[self dismissModalViewControllerAnimated:YES];


[tweetView setCompletionHandler:completionHandler];

- (void)viewWillAppear:(BOOL)animated
[super viewWillAppear:animated];

- (void)viewDidAppear:(BOOL)animated
[super viewDidAppear:animated];

- (void)viewWillDisappear:(BOOL)animated
[super viewWillDisappear:animated];

- (void)viewDidDisappear:(BOOL)animated
[super viewDidDisappear:animated];

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);



Step 8

Now we check the connection Outlet String. To do that we click on file owner and press the right mouse button. 


Step 9

Finally we click on the run button to show the output.

Step 10

Output 1 in iPhone:


Click on Select image Button.

Output 2 in iPhone:


Now choose an image from the Image Gallery. To do that click on the saved photos.

Output 3 in iPhone:


Now click on the Preview Tweet button.

Output 4 in iPhone:


To upload an image to Twitter we need a Twitter account. To do that we click on the Setting button which is shown in the alert view.

Output 5 in iPhone:


If you have an account on Twitter simply provide the user name and password and click on the sign-in button.

Output 6 in iPhone:


Output 7 in iPhone:


To add more accounts click on add account, otherwise click on your account.

Output 8 in iPhone:


Now again run your app and select an image.

Output 9 in iPhone:


Now tweet your message and photo.

Output 10 in iPhone:

Now to check your post on Twitter.


Click on the Safari icon.

Output 11 in iPhone:


Write URL

Output 12 in iPhone:


Check your post.

Output 13 in iPhone:


Here is my post.