1.11. Presenting Custom Sharing Options with UIActivityViewController
Problem
You want your app to participate in the list of apps that can handle sharing in iOS and appear in the list of available activities displayed in the activity view controller (see Figure 1-27).
You may need something like this, for example, when you have a text-editing app and when the user presses the Share button, you want a custom item that says “Archive” to appear in the activity view controller. When the user presses the Archive button, the text inside your app’s editing area will get passed to your custom activity, and your activity can then archive that text into the filesystem on the iOS device.
Solution
Create a class of type UIActivity. In other words, subclass the
aforementioned class and give a name (whatever you like) to your new
class. Instances of the subclasses of this class can be passed to the
initWithActivityItems:applicationActivities:
initializer of the UIActivityViewController class, and if they
implement all the required methods of the UIActivity class, iOS will display them in the
activity view controller.
Discussion
The initWithActivityItems:applicationActivities:
method’s first parameter accepts values of different types. These values
can be strings, numbers, images, etc.—any object, really. When you
present an activity controller with an array of arbitrary objects passed
to the initWithActivityItems parameter, iOS will go through all the available system activities, like Facebook and ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access