Identifying a selection on share sheet?

1k views Asked by At

I am using share sheet to open some of the native apps - i.e. Mail, Reminders, Notes, Messages. Below is my code to open share sheet after clicking on the share button.

I am looking for a way to identify a user click on share sheet i.e. an API that gets called when user selects an option on the share sheet. I searched and couldn't find anything, so not sure if there is a way to identify user selection?

func presentShareSheet(content: String, subject: String = "", shareButton: UIView? = nil, presentingVC: UIViewController? = nil) -> Guarantee<Void> {
    return Guarantee { seal in

        let itemsToShare: [Any] = [someItems]
        let activityViewController = UIActivityViewController(activityItems: itemsToShare, applicationActivities: nil)
 
        activityViewController.popoverPresentationController?.sourceView = shareButton
        activityViewController.completionWithItemsHandler = { activityType, completed, items, error in
            // update color of navigation bar
            seal(())
        }
        
        if let vc = presentingVC ?? UIApplication.shared.topMostViewController() {
            vc.present(activityViewController, animated: true)
        } else {
            seal(())
        }
    }
}
1

There are 1 answers

0
Joshua Dance On

The answer to your question as written is yes you can know what option a user chose.

UIActivityViewController - is there a way to know which activity was selected?

However in a comment you said you want something that 'get executed as soon as user selects 'Mail' not after they send or cancel'

Since the share sheet is provided by the iOS layer, I don't think there is anything that would run while the user is in the process of sharing. Only after.