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?
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);
}
};
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
}
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);
};
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)")
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With