Reader Level:
ARTICLE

Twitter in iPhone

Posted by Sachin Bhardwaj Articles | iOS December 04, 2012
In this article I will explain how to implement Twitter in an iPhone.
  • 0
  • 0
  • 3222
Download Files:
 

Introduction

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.

Build-phase-in-iphone.jpg

Step 3

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

frameworrk-in-iPhone.jpg

Step 4

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

twitter-framework-in-iPhone.jpg

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.

social-framework-in-iPhone.jpg

Step 6

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

mobilecoreservice-framework-in-iPhone.jpg

Step 7

Now we write the code for each class.

twitterappAppDelegate.h

#import <UIKit/UIKit.h>

@class twitterappViewController;

@interface twitterappAppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) twitterappViewController *viewController;

@end

twitterappAppDelegate.m

#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:.
}

@end

twitterappViewController.h

#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;

-(IBAction)previewTweet;
-(IBAction)selectImage;

@end

twitterappViewController.m

#import "twitterappViewController.h"

@implementation twitterappViewController
@synthesize tweetView, imageView;

- (void) previewTweet
{

[tweetView addImage:imageView.image];

[self presentModalViewController:tweetView animated:YES];
}

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

}

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

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");
break;
case TWTweetComposeViewControllerResultDone:
NSLog(@"Twitter Result: sent");
break;
default:
NSLog(@"Twitter Result: default");
break;
}
[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);
}
@end

twitterappViewController.xib

TwitterViewController.xib-in-iPhone.jpg

Step 8

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

linking-in-iPhone.jpg

Step 9

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

Step 10

Output 1 in iPhone:

output1-in-iPhone.jpg

Click on Select image Button.

Output 2 in iPhone:

output2-in-iPhone.jpg

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

Output 3 in iPhone:

output3-in-iPhone.jpg

Now click on the Preview Tweet button.

Output 4 in iPhone:

output4-in-iPhone.jpg

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:

output5-in-iPhone.jpg

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:

output6-in-iPhone.jpg

Output 7 in iPhone:

output7-in-iPhone.jpg

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

Output 8 in iPhone:

output8-in-iPhone.jpg

Now again run your app and select an image.

Output 9 in iPhone:

output9-in-iPhone.jpg

Now tweet your message and photo.

Output 10 in iPhone:

Now to check your post on Twitter.

output10-in-iPhone.jpg

Click on the Safari icon.

Output 11 in iPhone:

output11-in-iPhone.jpg

Write URL www.twitter.com

Output 12 in iPhone:

output12-in-iPhone.jpg

Check your post.

Output 13 in iPhone:

output13-in-iPhone.jpg

Here is my post.

COMMENT USING

Trending up