Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 707
Colton Anglin Avatar asked Feb 28 '14 00:02

Colton Anglin


2 Answers

Swift 5 - The below function covers most of the UIActivityViewController properties. It worked for me and thought so it might be helpful for you guys as well.

func performShareAction() {
        let itemsToShare : [Any] = ["Hello World"]
        let activityView = UIActivityViewController(activityItems: itemsToShare, applicationActivities: nil)
        
        // Apps that you want to exclude sharing the items
        let excludedActivityTypes : [UIActivity.ActivityType] = [
            .addToReadingList,
            .assignToContact,
            .copyToPasteboard,
            .mail,
            .markupAsPDF,
            .message,
            .openInIBooks,
            .postToFacebook,
            .postToFlickr,
            .postToTencentWeibo,
            .postToTwitter,
            .postToVimeo,
            .postToWeibo,
            .print,
            .saveToCameraRoll
        ]

        activityView.excludedActivityTypes = excludedActivityTypes
        self.present(activityView, animated: true, completion: nil)
        
        activityView.completionWithItemsHandler = { activityType, completed, items, error in
            
            // Event Cancelled
            if !completed {
                print("Content Sharing was cancelled.")
                return
            }
            
            // Content Shared on particular activity
            print("Shared on activity type: \(String(describing: activityType?.rawValue))")
            
            // Detect app on which the items are shared
            if let type = activityType {
                switch type {
                case .addToReadingList: print("Added To Reading List"); break
                case .airDrop: print("AirDropped to Other Device"); break
                case .assignToContact: print("Assigned To Contact"); break
                case .copyToPasteboard: print("Copied To Pasteboard"); break
                case .mail: print("Mailed"); break
                case .markupAsPDF: print("Marked-Up As PDF"); break
                case .message: print("Messaged"); break
                case .openInIBooks: print("Opened In iBooks"); break
                case .postToFacebook: print("Posted To Facebook"); break
                case .postToFlickr: print("Posted To Flickr"); break
                case .postToTencentWeibo: print("Posted To Tencent Weibo"); break
                case .postToTwitter: print("Posted To Twitter"); break
                case .postToVimeo: print("Posted To Vimeo"); break
                case .postToWeibo: print("Posted To Weibo"); break
                case .print: print("Printed"); break
                case .saveToCameraRoll: print("Saved To Camera Roll"); break
                default: print("Shared with new app"); break
                }
            }
        }
    }
like image 177
Saurabh Avatar answered Oct 05 '22 08:10

Saurabh


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)")
}
like image 36
Crashalot Avatar answered Oct 05 '22 07:10

Crashalot