Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
329 views
in Technique[技术] by (71.8m points)

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

I am implementing an application from which I have to share that applications on Facebook, Twitter as well as by mail. As my application is not a game, I just want to put an application icon, application name, iTunes link of that application and a small description of the application. I have implemented code which allow me to send a mail with attachment. Is that useful here? How can I do this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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];
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...