This post was originally published at Tutorial: Utilizing the Activity Popup Plugin (iOS)

Several weeks ago we published a tutorial illustrating how to implement iOS “Send To” using CoronaCards and Swift. In the following weeks, we also released the Activity Popup plugin, and now that all Corona SDK subscribers have access to this plugin, it’s a good time to explain its usage.

What is the Activity Popup?

Starting with iOS 7, Apple created a new view controller which can be accessed from other apps and which presents the user with a popup GUI containing icons for Message, Mail, Facebook, Twitter, flickr, Vimeo, etc. Selecting one of these icons allows you to share text, images, URLs, or other material depending on the service.

In technical terms, this is known as the UIActivityViewController and it presents sharing options based on the combination of links, images, and text which you specify. For instance, if you do not provide an image, you won’t see icons for iCloud Photo Sharing, flickr, or Vimeo.

Essentially, this plugin offers social sharing features in an easy-to-implement plugin and, as a side benefit, you can also use it to print, copy to other devices, and so forth.

Corona Implementation

In Corona SDK, the Activity Popup plugin is implemented via the native.showPopup() API call. Of course, because the core functionality is based on a plugin, you’ll need to include the necessary code within your build.settings file. Remember that this plugin is for iOS only, hence the inclusion of the supportedPlatforms table (if you’re targeting Android, you should use the Social Popup plugin instead).

Once the Activity Popup plugin is included, you’ll need to provide the native.showPopup() call with a table of items, where each item is its own table which defines a data type and related content information:

You can, of course, include multiple image, string, or url entries, but be aware that many services can only handle a limited numbers of items.

Next you need to set up a function that will show the activity popup. Typically this function will be triggered when the user presses a button or a similar on-screen element.

The code should be fairly straightforward. The showShare() function first checks if the popup can be displayed via the native.canShowPopup() call. Then, it performs a basic system.getInfo() check on the "environment" property to determine if the app is running in the Simulator or on an actual device. If the isAvailable flag is returned as true, the function proceeds to set up a listener function for the popup, then show the popup with the items table defined above.

In addition, there are two important properties to mention at this time: origin and permittedArrowDirections. On an iPhone, the activity UI will simply animate upwards from the bottom of the screen, so these properties are ignored. However, on an iPad, there is a much larger screen content to work with, so the activity UI can originate from many different points on the screen. This location is defined by the origin property, and typically it will be the button or other on-screen element that the user has triggered. It’s very important to note that this property should not be set to the actual display object, but rather the content bounds (edges) of the object — in other words, the plugin expects this to be a table of 4 numeric values: xMin, xMax, yMin, and yMax. Conveniently, Corona offers the object.contentBounds property which reveals these values in respect to the object itself.

Additionally, you can specify a table (permittedArrowDirections) that indicates which direction(s) the small UI arrow can point. If the origin point is along the bottom of the screen, it would be logical to include "down" in the permitted directions. Similarly, if the origin point is along the right side of the screen, the "right" arrow direction would be appropriate.

Finally, consider creating a button to trigger the activity popup. Note that we forward-declared this object above the showShare() function, because its reference is required within that function in order to pass its .contentBounds to the activity popup.

Limitations

The activity functionality built into iOS — and the functionality of this Activity plugin — is partially based on what the recipient app provides. In other words, this plugin supports only activities that the operating system knows about and thus it does not support Instagram or other 3rd-party options. For developers who require more custom control of activity options, Corona Enterprise is a viable option.


View original post here:

This post was originally published at Tutorial: Utilizing the Activity Popup Plugin (iOS)