iosswiftuiactivityviewcontroller

Detect which App was launched using UIActivityViewController


When an external app is launched using UIActivityViewController, can we detect which app was used in the completionHandler?

In Apple's documentation, there is mention of a property UIActivity.ActivityType, but it seems this is used only to detect "built-in activities". How can I detect if, eg. WhatsApp Messenger was launched?


Solution

  • When setting up your UIActivityViewController, use the completionWithItemsHandler to setup a completion closure that is called when the user makes a selection.

    This completion block takes four parameters: the (optional) selected activity, a completion indicator, an optional array of returned items, and an error.

    The selected activity is of type UIActivity.ActivityType. Its rawValue is a String representing the activity. If the activity type isn't one of the provided constants, compare its rawValue against a string you determine by running some tests to see its value for a given activity such as WhatsApp.