Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Figuring out which icon was clicked on in UIActivityViewController

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?

like image 455
sirab333 Avatar asked May 22 '13 00:05

sirab333


4 Answers

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);
    }
};
like image 133
rmaddy Avatar answered Sep 21 '22 10:09

rmaddy


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
}
like image 37
Yannick Avatar answered Sep 21 '22 10:09

Yannick


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);
};
like image 4
Jonny Avatar answered Sep 24 '22 10:09

Jonny


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)")
}
}
like image 4
kishorer747 Avatar answered Sep 21 '22 10:09

kishorer747