1

I have a case follows:

  • In app, i create a UIButton, when click will show a popup share facebook like UIActivityViewController. But i don't known get popup share facebook from UIActivityViewController. I need help everybody.

THANK YOU SO MUCH !!!

user3785457
  • 29
  • 2
  • 2

1 Answers1

6

You need to create a UIActivityViewController and specify the items you wish to share:

- (IBAction)buttonPressed:(id)sender
{
    NSString *textToShare = @"Put text here";
    UIImage *imageToShare = _img;
    NSArray *itemsToShare = @[textToShare, imageToShare];

    UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:itemsToShare applicationActivities:nil];
    [self presentViewController:activityViewController animated:TRUE completion:nil];
}

If you wish to exclude any activity types you can do so by adding:

// add an array of activity types to exclude
activityViewController.excludedActivityTypes = @[UIActivityTypeMail, UIActivityTypeAssignToContact, UIActivityTypePrint, UIActivityTypePostToTwitter, UIActivityTypePostToWeibo];

before presenting the activityViewController.

Also, to see the "share on Facebook" option, you need to be logged in to Facebook on the device, as stated in this answer.

///

Option 2 - without UIActivityController

If you do not want to use the Activity Controller, but instead just directly open the iOS social sharing dialog, you can do that by importing the Social framework and then using the Social Compose View Controller (SLComposeViewController)

// import Social framework
#import <Social/Social.h>    

// check if there is an account for Facebook
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])    
{

    SLComposeViewController *fbController=[SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];

    // set up a completion handler (optional)   
    SLComposeViewControllerCompletionHandler __block completionHandler=^(SLComposeViewControllerResult result){

        [fbController dismissViewControllerAnimated:YES completion:nil];

        switch(result){
            case SLComposeViewControllerResultCancelled:
            default:
                break;
            case SLComposeViewControllerResultDone:
                break;
    }};

    [fbController addImage:imageToPost];
    [fbController setInitialText:textToPost];
    [fbController addURL:urlToPost];
    [fbController setCompletionHandler:completionHandler];
    [self presentViewController:fbController animated:YES completion:nil];
}
Community
  • 1
  • 1
Ziga Dolar
  • 121
  • 4
  • Thank you for reply. But, i look some app, i see button share facebook when click only show popup facebook share of activityViewController, not show activityViewController. So... have you other way ? .. :( – user3785457 Nov 29 '14 at 13:31
  • I've edited my answer to include a direct sharing option using the Social framework. Guess I misunderstood, what you were trying to accomplish. Hope this helps. – Ziga Dolar Nov 29 '14 at 19:12