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

前端 未结 3 499
既然无缘
既然无缘 2020-11-28 13:59

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

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-28 14:49

    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 
    

    Set the delegate:

    UIViewController
    

    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 
    

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

提交回复
热议问题