How to share or post by mail, twitter and facebook from the current application?

1. For Facebook.

FBGraph is a much better way to use the Facebook API in your application.

Download the FBGraph API documents folder and then add it to in your folder. Read the instructions on the Facebook developer site.

This is the sample code and let me know if you have any query about it.

2. For EMail

Add MessageUI.framework in your project. Import the header file in your ViewController.h file:

 #import <MessageUI/MFMailComposeViewController.h>

Set the delegate:

UIViewController<MFMailComposeViewControllerDelegate>

And after that, open your mail composer like this:

-(void)yourEmailbuttonClick:(id)sender
{
    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
    picker.mailComposeDelegate = self;

    [picker setSubject:@"Hello!! your subject here"];

    // Set up recipients
    UIImage *image = [UIImage imageNamed:@"anyImage.png"];
    NSData *myData = UIImageJPEGRepresentation(image, 1.0);
    [picker addAttachmentData:myData mimeType:@"image/jpg" fileName:@"image"];
    [self presentModalViewController:picker animated:YES];
}


- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
    // Notifies users about errors associated with the interface
    switch (result)
    {
        case MFMailComposeResultCancelled:
            //        message.text = @"Result: canceled";
            break;
        case MFMailComposeResultSaved:
            //        message.text = @"Result: saved";
            break;
        case MFMailComposeResultSent:
            //            message.text = @"Result: sent";
            break;
        case MFMailComposeResultFailed:
            //            message.text = @"Result: failed";
            break;
        default:
            //            message.text = @"Result: not sent";
            break;
    }
    [self dismissModalViewControllerAnimated:YES];
}

3. For Twitter

Add Twitter.framework in your project. Import the header file in your ViewController.h file and import:

#import <Twitter/Twitter.h>

Now call the Twitter composer view like this:

-(void)yourTwitterbuttonClick:(id)sender
{
    if([TWTweetComposeViewController canSendTweet])
    {
        UIImage *image = [UIImage imageNamed:@"anyImage.png"];
        TWTweetComposeViewController *tweetViewController = [[TWTweetComposeViewController alloc] init];
        // Set initial text
        [tweetViewController setInitialText:@"your text here"];

        if (image)
        {
            [tweetViewController addImage: image];
        }

        tweetViewController.completionHandler = ^(TWTweetComposeViewControllerResult result)
        {
            if(result == TWTweetComposeViewControllerResultDone)
            {
                // The user finished composing a tweet
                alert.title=@"Status";
                alert.message=@"Tweet sent";
                [alert show];
            }
            else
                if(result == TWTweetComposeViewControllerResultCancelled)
                {
                    // The user cancelled composing a tweet
                    alert.title = @"Status";
                    alert.message = @"Tweet cancelled";
                    [alert show];
                }
            [self dismissViewControllerAnimated:YES completion:nil];
        };
        [self presentViewController:tweetViewController animated:YES completion:nil];
    }
}

Leave a Comment