0

I have a button to share my app's link. I want a dialog box for it. I have searched a lot for it but haven't found a satisfactory solution. How can I share my application link to different social platforms, like Facebook, Twitter or Gmail?

I'm using this code:

- (void)viewDidLoad {
    [super viewDidLoad];

    NSString *textToShare = @"Look at this awesome website for aspiring iOS Developers!";
    NSURL *myWebsite = [NSURL URLWithString:@"My URL"];

    NSArray *objectsToShare = @[textToShare, myWebsite];

    UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:objectsToShare applicationActivities:nil];

    NSArray *excludeActivities = @[UIActivityTypePostToFacebook,
                                   UIActivityTypePostToTwitter,
                                   UIActivityTypePostToFlickr,
                                   UIActivityTypePostToVimeo];

    activityVC.excludedActivityTypes = excludeActivities;

    [self presentViewController:activityVC animated:YES completion:nil];
    // Do any additional setup after loading the view.
}
Moshe
  • 57,511
  • 78
  • 272
  • 425
Hamza Imran
  • 189
  • 1
  • 18

1 Answers1

3

You can create your Share Action Button directly from Interface Builder and ctrl drag it into your code.

Then you can do something like this :

- (IBAction)shareByFacebook:(id)sender {

    if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
        SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];

        [self generateMessage:controller];

    }else{
        UIAlertView* facebookAlert = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"Social.Account.FB.title", @"") message:NSLocalizedString(@"Social.Account.FB.message", @"") delegate:nil cancelButtonTitle:NSLocalizedString(@"Error.ok", @"") otherButtonTitles: nil];
        [facebookAlert show];
    }
}

This method share an image and a corresponding text message to Facebook.

- (IBAction)shareByTwitter:(id)sender {
    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
    {
        SLComposeViewController *tweetSheet = [SLComposeViewController
                                               composeViewControllerForServiceType:SLServiceTypeTwitter];
        [self generateMessage:tweetSheet];

    }else{
        UIAlertView* twitterAlert = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"Social.Account.Twitter.title", @"") message:NSLocalizedString(@"Social.Account.Twitter.message", @"") delegate:nil cancelButtonTitle:NSLocalizedString(@"Error.ok", @"") otherButtonTitles: nil];
        [twitterAlert show];
    }
}

Same for Twitter.

Don't forget to import #import <Social/Social.h>

I have created a generic generateMessage method in order to avoid code repetition.

-(void)generateMessage:(SLComposeViewController *)controller
{

   if ([controller.serviceType isEqualToString:SLServiceTypeTwitter]) {
        NSString* message = @"The message you want."

        [controller setInitialText:message];
    }

    [controller setCompletionHandler:^(SLComposeViewControllerResult result) {
        if (result == SLComposeViewControllerResultDone) {
            DDLogInfo(@"Posted");
        } else if (result == SLComposeViewControllerResultCancelled) {
            DDLogInfo(@"Post Cancelled");
        } else {
            DDLogInfo(@"Post Failed");
        }
    }];

    [self.parentVC presentViewController:controller animated:YES completion:nil];
}

Those methods enable you to share content (images, photos, message..) to your Facebook/Twitter and Google account directly from your app.

N.B: For Google it's a little bit different because their share method is now deprecated

Share Google+ iOS

But you can use the old way, like this example in order to share an URL for example :

- (void)showGooglePlusShare:(NSURL*)shareURL {

    // Construct the Google+ share URL
    NSURLComponents* urlComponents = [[NSURLComponents alloc]
                                      initWithString:@"https://plus.google.com/share"];
    urlComponents.queryItems = @[[[NSURLQueryItem alloc]
                                  initWithName:@"url"
                                  value:[shareURL absoluteString]]];
    NSURL* url = [urlComponents URL];

    if ([SFSafariViewController class]) {
        // Open the URL in SFSafariViewController (iOS 9+)
        SFSafariViewController* controller = [[SFSafariViewController alloc]
                                              initWithURL:url];
        controller.delegate = self;
        [self.parentVC presentViewController:controller animated:YES completion:nil];
    } else {
        // Open the URL in the device's browser
        [[UIApplication sharedApplication] openURL:url];
    }
}

EDIT :

You can create only 1 IBAction button in order to share to social network. And then the user has to choose which one.

The result will be something like this :

enter image description here

And the code example :

- (IBAction)shareContentSocialNetwork:(id)sender
{
    if ([UIAlertController class]){
        // ios 8 or higher
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"" message:@"Share on Social Network" preferredStyle:UIAlertControllerStyleActionSheet];

        UIAlertAction* fb = [UIAlertAction actionWithTitle:@"Facebook" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action)
                             {
                                 if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
                                     SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
                                  // Create a method in order to add image, text etc..
                                  [self generateMessage:controller];

                                 }else{
                                     UIAlertView* facebookAlert = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"Social.Account.FB.title", @"") message:NSLocalizedString(@"Social.Account.FB.message", @"") delegate:nil cancelButtonTitle:NSLocalizedString(@"Error.ok", @"") otherButtonTitles: nil];
                                     [facebookAlert show];
                                 }
                             }];

        [alertController addAction:fb];

        UIAlertAction* twit = [UIAlertAction actionWithTitle:@"Twitter" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action)
                               {
                                   if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
                                   {
                                       SLComposeViewController *tweetSheet = [SLComposeViewController
                                                                              composeViewControllerForServiceType:SLServiceTypeTwitter];
                                      // Create a method in order to add image, text etc..
                                      [self generateMessage:controller];

                                   }else{
                                       UIAlertView* twitterAlert = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"Social.Account.Twitter.title", @"") message:NSLocalizedString(@"Social.Account.Twitter.message", @"") delegate:nil cancelButtonTitle:NSLocalizedString(@"Error.ok", @"") otherButtonTitles: nil];
                                       [twitterAlert show];
                                   }
                               }];
        [alertController addAction:twit];

        UIAlertAction* ggl = [UIAlertAction actionWithTitle:@"Google+" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action)
                              {
                                  NSURL *url = [[NSURL alloc] initWithString:@"yourContentURL"];
                                  [self showGooglePlusShare:url];
                              }];
        [alertController addAction:ggl];

        UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
        [alertController addAction:cancel];

        [self presentViewController:alertController animated:YES completion:nil];

    }
}

Basically I am creating the 3 specific actions of the AlertController. For Twitter and Facebook it is pretty straightforward, even so you have to use the generateMessage method I showed you earlier.

Hope it helps.

  • how can we show different social media icons so that user could select it and further process occurs. @Janna – Hamza Imran Jul 03 '17 at 15:11
  • i have share button in left drawer and i want it when user click the button different social media option should be shown to share link of app and when user click any of them than it could share easily on that .@Janna – Hamza Imran Jul 03 '17 at 15:13
  • Ok so my solution is for 3 different buttons, but let me edit the answer for choice selection. –  Jul 03 '17 at 15:14
  • can i use image rather than facebook keyword? @Janna – Hamza Imran Jul 03 '17 at 18:11
  • what is generatMessage and CDImageOfTheDay in ur code? @Janna – Hamza Imran Jul 03 '17 at 18:20
  • Yes you can customize your UIAlertAction. You can see some example https://stackoverflow.com/questions/26347085/add-image-to-uialertaction-in-uialertcontroller. –  Jul 04 '17 at 11:19
  • CDImageOfTheDay is from my project, it was a mistake. The generatMessage message method is to add the content you want to share to the controller (Facebook/Twitter etc..), which text/image. I showed you an example. –  Jul 04 '17 at 11:21