3

How can I get selected activity name? I can only get activity type from completionWithItemsHandler.

Here is an example:

let activityViewController = UIActivityViewController(activityItems: items,
                                                      applicationActivities: nil)
activityViewController.completionWithItemsHandler = { activity, success, items, error in
    if let activity = activity {
        print("ACTIVITY IS \(activity.rawValue)")
    }
}
viewController.present(activityViewController, animated: true, completion: nil)

I want to get let says "Mail" or "Message" but the code above gives me something like com.apple.UIKit.activity.Message or com.apple.UIKit.activity.Mail

pacification
  • 5,838
  • 4
  • 29
  • 51
user1526474
  • 925
  • 13
  • 26
  • There is no API to get the activity name from an `activityType` (other than from your own custom application activities of course). – rmaddy Sep 10 '18 at 03:14
  • Please have a look at here: https://stackoverflow.com/questions/30063708/set-different-activity-items-for-uiactivityviewcontroller-swift, this is something you can try. – Bhavin Kansagara Sep 10 '18 at 04:49
  • @BhavinKansagara Can you explain how that other question and its answers apply to this question? Also keep in mind that there are several standard, Apple provided activities that do not have corresponding `UIActivityType` entries. – rmaddy Sep 10 '18 at 05:34
  • https://stackoverflow.com/questions/30063708/set-different-activity-items-for-uiactivityviewcontroller-swift/44796721#44796721 in his answer, he is using the if else conditions to check for the type and retuning the name of it. So, by similar approach one can create a function, which takes in the activity type and returns the human readable names like Mail, Message etc... That's why I pointed to that question. – Bhavin Kansagara Sep 10 '18 at 05:43
  • @BhavinKansagara In other words, hardcode your own mapping of activity names to activity types and possibly do so in multiple languages as well as deal with new and undocumented activity types that may appears in each new version of iOS. – rmaddy Sep 10 '18 at 06:26
  • You are right at your point, it is tough to handle it with hardcode for newer iOS versions, but for the purpose to be served, it can be hardcoded for the available types and in else case, return the rawValue. This was just a suggestion, however, ultimate choice is on developer to choose from the available possible options. – Bhavin Kansagara Sep 10 '18 at 07:22
  • @BhavinKansagara This is good idea but it will only work for the predefined UIActivityType entries. Many apps can define an activity type which won't be handled this way. – Petar Jan 18 '21 at 15:27

0 Answers0