Figuring out which icon was clicked on in UIActivityViewController
Solution 1
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);
}
};
Solution 2
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
}
Solution 3
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);
};
Solution 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)")
}
}
Solution 5
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.
}
}];
sirab333
Updated on July 27, 2022Comments
-
sirab333 almost 2 years
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?
-
sirab333 almost 11 yearsYeah 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?
-
rmaddy almost 11 yearsYou should have made your desire clear in your original question. You would have gotten much better results. See stackoverflow.com/questions/13551042/…
-
sirab333 almost 11 yearsThank you for the link - I'll check it out!
-
Zorayr over 10 years@sirab333 you should probably just have different buttons for each type - would make your life much easier.
-
Rushi trivedi almost 10 years@rmaddy how can i add different text to facebook and twitter ?? any idea?
-
ramesh bhuja about 8 yearsThis 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 ?