Search This Blog

Jul 8, 2011

How to send text to twitter account using iPhone SDK ?

This example shows how to send text to Twitter account using iPhone SDK this example using OAuth.


You can download source code from here Download.

Code for .h file.

#import
#import "SA_OAuthTwitterController.h"

@class SA_OAuthTwitterEngine;

@interface UITwitterSharingController : UIViewController
{
    IBOutlet UIView *viewTextView;
    IBOutlet UITextView *txtViewDescription;
  
    SA_OAuthTwitterEngine *_engine;
    NSString *oauth_token;
    NSString *oauth_token_secret;
}

- (IBAction)btnShare_Clicked:(id)sender;

- (void)btnDone_Clicked:(id)sender;
- (void)authenticateInAppViaTwitter;
- (void)postInAppTwitter;

@end

Code for .m file.

#import "UITwitterSharingController.h"
#import "SA_OAuthTwitterEngine.h"

#define kOAuthConsumerKey @"78lKbzufPV9F7WmfvhUEw"
#define kOAuthConsumerSecret @"0vDpOuE7Y6knvRLCQ2zruc1q2oTkFNTXHW1dwEU"

@implementation UITwitterSharingController

int intTwitterFlag;

- (void)viewDidLoad {
    [super viewDidLoad];
  
    self.navigationController.navigationBar.hidden = TRUE;
    txtViewDescription.font = [UIFont systemFontOfSize:13];
    intTwitterFlag = 0;
}

- (IBAction)btnShare_Clicked:(id)sender
{
    [self authenticateInAppViaTwitter];
}

- (void)authenticateInAppViaTwitter
{  
    intTwitterFlag = 0;
  
    if(!_engine)
    {
        _engine = [[SA_OAuthTwitterEngine alloc] initOAuthWithDelegate:self];
        _engine.consumerKey    = kOAuthConsumerKey;
        _engine.consumerSecret = kOAuthConsumerSecret;  
    }
  
    UIViewController *controller = [SA_OAuthTwitterController controllerToEnterCredentialsWithTwitterEngine:_engine delegate:self];
  
    if (controller)
    {
        [self presentModalViewController: controller animated: YES];
        intTwitterFlag = 1;
    }
  
    if (intTwitterFlag == 0)
    {
        [self performSelector:@selector(postInAppTwitter)withObject:nil afterDelay:1.0];
    }
}

- (void)postInAppTwitter
{  
    [_engine sendUpdate:txtViewDescription.text];
}

//=============================================================================================================================
#pragma mark SA_OAuthTwitterEngineDelegate
- (void) storeCachedTwitterOAuthData: (NSString *) data forUsername: (NSString *) username
{
    NSUserDefaults            *defaults = [NSUserDefaults standardUserDefaults];
  
    [defaults setObject: data forKey: @"authData"];
    [defaults synchronize];
  
    if (intTwitterFlag == 1)
    {  
        [self performSelector:@selector(postInAppTwitter)withObject:nil afterDelay:1.0];
    }
}

- (NSString *) cachedTwitterOAuthDataForUsername: (NSString *) username
{
    return [[NSUserDefaults standardUserDefaults] objectForKey: @"authData"];
}

//=============================================================================================================================
#pragma mark TwitterEngineDelegate
- (void) requestSucceeded: (NSString *) requestIdentifier
{
  
    NSLog(@"Request %@ succeeded", requestIdentifier);
  
    UIAlertView *alrtView = [[UIAlertView alloc]initWithTitle:@"Tweet posted on your twitter account" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Ok", nil];
    [alrtView show];
    [alrtView release];
}

- (void) requestFailed: (NSString *) requestIdentifier withError: (NSError *) error
{
  
    NSLog(@"Request %@ failed with error: %@", requestIdentifier, error);
  
    UIAlertView *alrtView = [[UIAlertView alloc]initWithTitle:@"Tweet did not posted" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Ok", nil];
    [alrtView show];
    [alrtView release];
}

- (void)textViewDidBeginEditing:(UITextView *)textView
{
    [self.view addSubview:viewTextView];
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
    CGRect rect = self.view.frame;
    //rect.origin.y = self.view.frame.origin.y - 174;
    rect.origin.y = self.view.frame.origin.y - 0;
    self.view.frame = rect;
    viewTextView.frame = CGRectMake(0, 200, 320, 44);
    //viewTextView.frame = CGRectMake(0, 374, 320, 44);
    [UIView commitAnimations];
}

- (void)btnDone_Clicked:(id)sender
{
    [txtViewDescription resignFirstResponder];
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
    CGRect rect = self.view.frame;
    //rect.origin.y = self.view.frame.origin.y + 174;
    rect.origin.y = self.view.frame.origin.y + 0;
    self.view.frame = rect;
    viewTextView.frame = CGRectMake(0, 460, 320, 44);
    [UIView commitAnimations];
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
  
    // Release any cached data, images, etc that aren't in use.
}

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


- (void)dealloc {
    [super dealloc];
}


@end

No comments:

Post a Comment