UIActivityViewController completion handler still calls action if user presses cancel
Note: the completionHandler property is deprecated in iOS8, so it's not possible anymore to know the result of a share action.
https://developer.apple.com/documentation/uikit/uiactivityviewcontroller/1622010-completionhandler
Update: Like adruzh said, on iOS8 there's a new completionHandler that Apple forgot to mention in the documentation:
[activityController setCompletionWithItemsHandler:
^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) {
}];
https://developer.apple.com/documentation/uikit/uiactivityviewcontroller/1622022-completionwithitemshandler
That's what the completed
argument is for:
[controller setCompletionHandler:^(NSString *activityType, BOOL completed) {
if (!completed) return;
CWStatusBarNotification *notification = [CWStatusBarNotification new];
[notification displayNotificationWithMessage:@"✓ Successfully Shared Centre!"
forDuration:3.0f];
notification.notificationLabelBackgroundColor = [UIColor colorWithRed:38.0f/255.0f green:81.0f/255.0f blue:123.0f/255.0f alpha:1.0f];
notification.notificationLabelTextColor = [UIColor whiteColor];
}];
For Swift, this is what worked for us:
...
// 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)")
}