Search code examples
iosuiactivityviewcontrollercompletionhandler

UIActivityViewController completion handler still calls action if user presses cancel


In my UIActivityViewController, I use completion handler to execute a "successfully shared" notification. It works but my only problem is, it still shows the notification if the user presses cancel.

Here is my completion handler code,

[controller setCompletionHandler:^(NSString *activityType, BOOL completed) {


    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];



}];

Thanks for the help!


Solution

  • 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];
    }];