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:






Integrate a FaceBook Code for iOS

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

Step 2 : Add the header files are  -- >       #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 * FaceBook;
}

-(IBAction)FaceBook:(id)sender;

@end.



.m File
-(IBAction) FaceBook:(id)sender
{
            SLComposeViewController *fbController=[SLComposeViewController
composeViewControllerForServiceType:SLServiceTypeFacebook];
 
 
    if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
    {
        SLComposeViewControllerCompletionHandler __block completionHandler=^(SLComposeViewControllerResult result){
         
            [fbController dismissViewControllerAnimated:YES completion:nil];
         
            switch(result){
                case SLComposeViewControllerResultCancelled:
                default:
                {
                    NSLog(@"Cancelled.....");
                    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Message" message:@"Cancelled" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles: nil];
                    [alert show];
                    [alert release];
                }
                    break;
                case SLComposeViewControllerResultDone:
                {
                    NSLog(@"Posted....");
                    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Message" message:@"Message Posted Succesfully" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles: nil];
                    [alert show];
                    [alert release];
                }
                    break;
            }};
         fbController.completionHandler =completionHandler;
        [fbController addImage:[UIImage imageNamed:@"sample.png"]];
        [fbController setInitialText:@"Welcome to iPhone Developement Website."];
        [fbController addURL:[NSURL URLWithString:@"http://asanhussain.blogspot.in/"]];
        [fbController setCompletionHandler:completionHandler];
        [self presentViewController:fbController animated:YES completion:nil];
    }
    else{
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Message" message:@"Configure Your Setting" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
        NSLog(@"UnAvailable");
    }

}



.xib File & Output: