Determine which extension was used

Since completionHandler on UIActivityViewController deprecated in iOS 8, is there a way to determine which share extension / activity was used by the user?

+5
source share
3 answers

You just need to use the new UIActivityViewControllerCompletionWithItemsHandler handler:

 typedef void (^UIActivityViewControllerCompletionWithItemsHandler)(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError); 

Name it as follows:

 [yourActivityVC setCompletionWithItemsHandler:^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError){ }]; 

In addition, if you are interested in knowing which item was selected, you just need to make sure that your activity items comply with the UIActivityItemSource protocol;

UIActivityItemSource Documentation

+14
source

setCompletionWithItemsHandler is iOS8 only. If you need to support iOS6-7, then:

 [yourActivityVC setCompletionHandler:^(NSString *activityType, BOOL completed){ }]; 
+1
source

This is what we used in Swift:

  ... // Configure UIActivityViewController let activityViewController = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil) activityViewController.excludedActivityTypes = [UIActivityTypeAirDrop, UIActivityTypeAddToReadingList, UIActivityTypeAssignToContact, UIActivityTypePrint, UIActivityTypeCopyToPasteboard] // Show UIActivityViewController presentViewController(activityViewController, animated: true, completion: nil) // Define completion handler activityViewController.completionWithItemsHandler = doneSharingHandler ... func doneSharingHandler(activityType: String?, completed: Bool, returnedItems: [AnyObject]?, error: NSError?) { // Return if cancelled if (!completed) { return } // If here, log which activity occurred println("Shared video activity: \(activityType)") } 
+1
source

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


All Articles