iOS | Code | How-To Program an iOS Twitter App

With IOS 5 SDK Apple has introduced an API to allow a developer to incorporate a Twitter interface in their apps.The API offers two interfacing options: to get information from Twitter using Twitter extensive API by implementing the TWRequest class or sending tweets to Twitter by implementing the TWTweetComposeViewController to create a sheet.

To implement the API, create a Single View application. Add the Twitter Framework to the project by selecting the Project root in scrolling to the Linked Frameworks and Libraries section on the Summary page. Click on the plus symbol and in the search field enter twitter ( see screenshot below):

Twitter Framework in Project
Twitter Framework in Project
Add Twitter Framework
Add Twitter Framework

You will have to manually drag the inserted framework into the Frameworks group.

Add Button and IBAction

Next Open the Storyboard file and add a Button to the Canvas. To create a IBAction connection, open the Assistant Editor and drag+drop a connection from the button to the open header file. In the Popover, change the Connection to IBAction, give your connection a name. Finally change the sender action to “Touch Up Inside”.

SendATweet

Next open the ViewController file, to which we will add an import statement in the both the header and implementation files for the Twitter API. Add an instance method SendATweet to the header file.

In the implementation file implement the SendATweet instance method.

Since the TWTweetComposeViewController tweet cannot be declare as strong since it would generate a retain cycle, so we will define or implement it in our implementation file in the SendATweet.

TWTweetComposeViewController * tweetVC = [[TWTweetComposeViewController alloc] init];


Optionally you can add an URL, text and image. In our example I will an URL and initial text. Note that once the TWTweetComposeViewController is displayed, you won’t be able to change the data, only the user will be able to.

-(void) SendATweet {
 TWTweetComposeViewController * tweetVC = [[TWTweetComposeViewController alloc] init];
 tweetURL = [[NSURL alloc] initWithString:@"http://klanguedoc.hubpages.com"];
 [tweetVC addURL:tweetURL];
 [tweetVC addInitialText:@”Here is some sample text from @kevlangdo”];

Now we will load the tweetVC TWTweetComposeViewController view controller. Once the use has edited the text, location and / image they will either click on the “Send” button or the “Cancel”. We will capture these events next by calling the completionHandler method of the TWTweetComposeViewController View Controller.

[self presentViewController:tweetVC animated:YES completion:nil];

tweetVC.completionHandler = ^(TWTweetComposeViewControllerResult twResult) {
switch(twResult) {

case TWTweetComposeViewControllerResultDone:
// This means the user hit 'Send'
break;
case TWTweetComposeViewControllerResultCancelled:
// This means the user cancelled without sending the Tweet
break;
}

Once the user has taking an action, we will need to dismiss the TWTweetComposeViewController View Controller and the keyboard. We will need to call the dismissViewControllerAnimated method of the ViewController class to send the Twitter Composer to the background.

//Dismiss the TWTweetComposeViewController
dispatch_async(dispatch_get_main_queue(), ^{
[self dismissViewControllerAnimated:NO completion:nil];
});

};
}

All that is left is to add a bit of code to call the SendATweet from the IBAction button:

- (IBAction)TweetAction:(id)sender {
[self SendATweet];
}


That is it. It is so simple. The first time you run this app in the Simulator, you will need to setup your account in the App settings app in the App Settings in the Simulator.:

Setup Twitter Account in Settings in Simulator
Setup Twitter Account in Settings in Simulator
TWTweetComposeViewController
TWTweetComposeViewController

klViewController.m

//
//  klViewController.m
//  TweetThis
//
//  Created by Kevin Languedoc on 2/16/12.
//  Copyright (c) 2012 kCodebook. All rights reserved.
//

#import "klViewController.h"
#import <Twitter/Twitter.h>

@implementation klViewController
@synthesize tweetURL;


- (IBAction)TweetAction:(id)sender {
      [self SendATweet];
}

-(void) SendATweet{
    
    TWTweetComposeViewController * tweetVC = [[TWTweetComposeViewController alloc] init];
    
   // if([TWTweetComposeViewController canSendTweet]){
        tweetURL = [[NSURL alloc] initWithString:@"http://twitter.com/"];
        [tweetVC addURL:tweetURL];
        [self presentViewController:tweetVC animated:YES completion:nil];   
        
        tweetVC.completionHandler = ^(TWTweetComposeViewControllerResult twResult) {
            switch(twResult) {
                    
                case TWTweetComposeViewControllerResultDone:
                    //  This means the user hit 'Send'
                    break;
                case TWTweetComposeViewControllerResultCancelled:
                    //  This means the user cancelled without sending the Tweet
                    break;
            }
            
            //Dismiss the TWTweetComposeViewController
            dispatch_async(dispatch_get_main_queue(), ^{            
                [self dismissViewControllerAnimated:NO completion:nil];
            });
            
        };

   // }

}
- (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.

    
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (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 YES;
}

@end

klViewController.h

//
//  klViewController.h
//  TweetThis
//
//  Created by Kevin Languedoc on 2/16/12.
//  Copyright (c) 2012 kCodebook. All rights reserved.
//

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

@interface klViewController : UIViewController


@property(nonatomic, strong) NSURL *tweetURL;

- (IBAction)TweetAction:(id)sender;

-(void) SendATweet;


@end
Twitter Output
Twitter Output

More by this Author


Comments 5 comments

mikeydcarroll67 4 years ago

Thanks for posting this! I'll definitely need it!


klanguedoc profile image

klanguedoc 4 years ago from Canada Author

You very welcome. The API is very easy to work with, you will enjoy it.


mikeydcarroll67 4 years ago

App is finished and submitted, is there any way to do something similar with the Facebook framework? Like submitting a URL to a Facebook status?


klanguedoc profile image

klanguedoc 4 years ago from Canada Author

Yes there is a SDK on the Facebook Developer site. You have to register a Facebook App to get an id before you can implement the SDK.


mikeydcarroll67 4 years ago

OK. I'll look into that!

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working