Wednesday, 3 October 2012

Integrate a Twitter Code for iOS

Step 1 : Add the Frame Works are  -- >   1. Twitter.framework    
                                                                  2. Social.framework


Step 2 : Add the header files are  -- >      1. #import <Twitter/Twitter.h>
                                                                 2. #import <Social/Social.h>



Step 3 : Add the Coding are

.h File


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

@interface sample : UIViewController
{
         IBOutlet UIButton * Twitter;
}

-(IBAction)Twitter:(id)sender;

@end.



.m File
-(IBAction)Twitter:(id)sender
{

           if (NSClassFromString(@"TWTweetComposeViewController")) {
     
        NSLog(@"Twitter framework is available on the device");
        //code goes here
        //create the object of the TWTweetComposeViewController
        TWTweetComposeViewController *twitterComposer = [[TWTweetComposeViewController alloc]init];
     
        NSString * display=[NSString stringWithFormat:@"Type some text about Your app and share with Twitter"];
     
        //set the text that you want to post
        [twitterComposer setInitialText:display];
     
        //add Image
        //[twitterComposer addImage:[UIImage imageNamed:@"sample.png"]];
     
        //add Link
        // [twitterComposer addURL:[NSURL URLWithString:@"asanhussain.blogspot.in"]];
     
        //display the twitter composer modal view controller
        [self presentModalViewController:twitterComposer animated:YES];
     
        //check to update the user regarding his tweet
        twitterComposer.completionHandler = ^(TWTweetComposeViewControllerResult res){
         
            //if the posting is done successfully
            if (res == TWTweetComposeViewControllerResultDone){
                UIAlertView * objAlertView = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"Tweet successful" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
                [objAlertView show];
                [objAlertView release];
            }
            //if posting is done unsuccessfully
            else if(res==TWTweetComposeViewControllerResultCancelled){
                objAlertView = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"Tweet unsuccessful" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
                [objAlertView show];
                [objAlertView release];
            }
            //dismiss the twitter modal view controller.
            [self dismissModalViewControllerAnimated:YES];
            //[tweetTextField resignFirstResponder];
        };
     
     
     
        //releasing the twiter composer object.
        [twitterComposer release];
     
    }else{
        NSLog(@"Twitter framework is not available on the device");
     
        objAlertView = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"Your device cannot send the tweet now, kindly check the internet connection or make a check whether your device has atleast one twitter account setup" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [objAlertView show];
        [objAlertView release];
     
    }
}



.xib File & Output:






No comments: