9

Using the "Social" Framework, when presenting the modal UIActivityViewController that displays all the usual social-media icons, is there a way to find out exactly which icon the user clicked on? Meaning, if they chose Twitter, Facebook, Mail, Message, etc?

I was expecting to possibly see some delegate methods for this class in the Docs but I don't see anything.

Anybody got any ideas?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
sirab333
  • 3,662
  • 8
  • 41
  • 54

5 Answers5

17

The UIActivityViewController has the completionHandler property. Implement this handler to be notified.

UIActivityViewController *avc = ...;
avc.completionHandler = ^(NSString *activityType, BOOL completed) {
    if (completed) {
        NSLog(@"The selected activity was %@", activityType);
    }
};
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • 3
    Yeah but that gets triggered AFTER the post is complete - which is too late for my purposes. What I'm doing is pre-populating the Tweet (or Facebook post, email msg., etc.) that the user is sending with some text. Except that piece of text will be different for Tweets, emails, etc. So I thought that as soon as the user clicks on the icon, I can then programmatically change the text. Any ideas on how to do that? – sirab333 May 22 '13 at 01:39
  • You should have made your desire clear in your original question. You would have gotten much better results. See http://stackoverflow.com/questions/13551042/different-data-for-sharing-providers-in-uiactivityviewcontroller – rmaddy May 22 '13 at 02:07
  • Thank you for the link - I'll check it out! – sirab333 May 22 '13 at 13:54
  • @sirab333 you should probably just have different buttons for each type - would make your life much easier. – Zorayr Dec 27 '13 at 05:51
  • @rmaddy how can i add different text to facebook and twitter ?? any idea? – Rushi trivedi May 17 '14 at 07:22
7

Swift 3

The typealias UIActivityViewControllerCompletionWithItemsHandler in Swift 3 changed to

public typealias UIActivityViewControllerCompletionWithItemsHandler = (UIActivityType?, Bool, [Any]?, Error?) -> Swift.Void

You can use the completionWithItemsHandler like this.

activityViewController.completionWithItemsHandler = {(activityType: UIActivityType?, completed: Bool, returnedItems:[Any]?, error: Error?) in
    //Do whatever you want
}
Yannick
  • 3,210
  • 1
  • 21
  • 30
4

completionHandler is deprecated. Use completionWithItemsHandler.

activityController.completionWithItemsHandler = ^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) {
    NSLog(@"completionWithItemsHandler, activityType: %@, completed: %d, returnedItems: %@, activityError: %@", activityType, completed, returnedItems, activityError);
};
Jonny
  • 15,955
  • 18
  • 111
  • 232
4

Based on this SO answer: https://stackoverflow.com/a/34581940/2177085

This also include to check is user shared through WhatsApp or Gmail. You can print activityType to add other types.

let activityViewController = UIActivityViewController(activityItems: [finalMsg as NSString], applicationActivities: nil)
self.presentViewController(activityViewController, animated: true, completion: nil)

activityViewController.completionWithItemsHandler = {(activityType, completed:Bool, returnedItems:[AnyObject]?, error: NSError?) in

// Return if cancelled
if (!completed) {
    print("user clicked cancel")
    return
}

if activityType == UIActivityTypeMail {
    print("share throgh mail")
}
else if activityType == UIActivityTypeMessage {
    print("share trhought Message IOS")
}
else if activityType == UIActivityTypePostToTwitter {
    print("posted to twitter")
}
else if activityType == UIActivityTypePostToFacebook {
    print("posted to facebook")
}
else if activityType == UIActivityTypeCopyToPasteboard {
    print("copied to clipboard")
}
else if activityType! == "net.whatsapp.WhatsApp.ShareExtension" {
    print("activity type is whatsapp")
}
else if activityType! == "com.google.Gmail.ShareExtension" {
    print("activity type is Gmail")
}
else {
    // You can add this activity type after getting the value from console for other apps.
    print("activity type is: \(activityType)")
}
}
Community
  • 1
  • 1
kishorer747
  • 810
  • 1
  • 10
  • 24
2

The below snippet worked in my case:

[activityController setCompletionHandler:^(NSString *act, BOOL done)
{

   NSLog(@"act type %@",act);
   NSString *ServiceMsg = nil;
   if ( [act isEqualToString:UIActivityTypeMail] )
     ServiceMsg = @"Mail sent";

   if ( [act isEqualToString:UIActivityTypePostToTwitter] )
     ServiceMsg = @"Post on twitter, ok!";

   if ( [act isEqualToString:UIActivityTypePostToFacebook] ) 
     ServiceMsg = @"Post on facebook, ok!";

   if ( done )
   {
      UIAlertView *Alert = [[UIAlertView alloc] initWithTitle:ServiceMsg message:@"" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil];
      [Alert show];
      [Alert release];
   }
   else
   {
      // didn't succeed. 
   }
}];
Community
  • 1
  • 1
Napster
  • 120
  • 6
  • This Method is Completion method. but i want to get when i click on Icon like FB or whatsapp than i wnat to get click event. so how can i do this ? – ramesh bhuja Apr 25 '16 at 07:58