Finding out which icon the UIActivityViewController icon was clicked on

Using the “social” structure, when presenting a modal UIActivityViewController that displays all the usual social media icons, is there any way to find out which icon the user clicked on? Sense if they chose Twitter, Facebook, Mail, Message, etc.?

I expected to see some delegate methods for this class in Docs, but I don’t see anything.

Does anyone have any ideas?

+6
source share
5 answers

UIActivityViewController has a completionHandler property. Add this handler for notification.

 UIActivityViewController *avc = ...; avc.completionHandler = ^(NSString *activityType, BOOL completed) { if (completed) { NSLog(@"The selected activity was %@", activityType); } }; 
+17
source

completionHandler deprecated. Use completionWithItemsHandler .

 activityController.completionWithItemsHandler = ^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) { NSLog(@"completionWithItemsHandler, activityType: %@, completed: %d, returnedItems: %@, activityError: %@", activityType, completed, returnedItems, activityError); }; 
+3
source

The following 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. } }]; 
+2
source

Based on this answer SO: fooobar.com/questions/278017 / ...

It also includes checking user access through WhatsApp or Gmail. You can print actionType 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)") } } 
+2
source

Swift 3

UIActivityViewControllerCompletionWithItemsHandler UIActivityViewControllerCompletionWithItemsHandler in Swift 3 changed to

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

You can use the completion functionWithItemsHandler as follows.

 activityViewController.completionWithItemsHandler = {(activityType: UIActivityType?, completed: Bool, returnedItems:[Any]?, error: Error?) in //Do whatever you want } 
+2
source

Source: https://habr.com/ru/post/945532/


All Articles