Missing piece of head first book chapter two – Sending tweet using iOS

I have been following iOS development using Head First book.  If you have completed the second chapter, than you would have surely landed up with the problem that sending tweet does not work.

The reason why sending tweet does not work is because book was written way back and Twitter moved to OAuth authentication afterwards.

The simplest way to enable tweeting is to use Twitter Library in built in iOS framework.  

Add the twitter framework in project. Change the code of send button to look like this

 

-(IBAction)sendButtonTapped:(id)sender

{

    NSString* theMessage = [NSString stringWithFormat:@”I’m %@ and feeling %@ about it.”, [activities objectAtIndex:[tweetPicker selectedRowInComponent: 0]], [feelings objectAtIndex:[tweetPicker selectedRowInComponent:1]]];

    

    NSLog(theMessage);

    

    if([TWTweetComposeViewController canSendTweet])

    {

        NSLog(@”inside cansend”);

        

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

        [vc setInitialText:theMessage];

    

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

    }

    else {

        // Show Alert View When The Application Cannot Send Tweets

        NSString *message = @”The application cannot send a tweet at the moment. This is because it cannot reach Twitter or you don’t have a Twitter account associated with this device.”;

        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@”Oops” message:message delegate:nil cancelButtonTitle:@”Dismiss” otherButtonTitles:nil];

        [alertView show];

    }

   

}

 

Some key points about this code:

1. It checks for internet connectivity and if its not there, app informs the user with a message.

2. The beauty is twitter framework is integrated within iOS, after clicking on send you will see a dialog.

Enjoy and have fun with your Head First book, it is truly a great book.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s