iOS Creating a NSUserActivity


Example

To create a NSUserActivity object, your app must declare the types of activities it supports in its Info.plist file. Supported activities are defined by your application and should be unique. An activity is defined using a reverse-domain style naming scheme (i.e. "com.companyName.productName.activityName"). Here is what an entry in your Info.plist might look like:

KeyValue
NSUserActivityTypes[Array]
- item0com.companyName.productName.activityName01
- item1com.companyName.productName.activityName02

Once you have defined all supported activity types, you may begin to access and use them in your application's code.

To create a NSUserActivity object you must do the following

// Initialize the activity object and set its type from one of the ones specified in your app's plist
NSUserActivity *currentActivity = [[NSUserActivity alloc] initWithActivityType:@"com.companyName.productName.activityName01"];

// Set the title of the activity.
// This title may be displayed to the user, so make sure it is localized and human-readable
currentActivity.title = @"Current Activity";

// Configure additional properties like userInfo which will be included in the activity
currentActivity.userInfo = @{@"informationKey" : @"value"};

// Configure the activity so the system knows what may be done with it
// It is important that you only set YES to tasks that your application supports
// In this example, we will only enable the activity for use with Handoff
[currentActivity setEligibleForHandoff:YES];
[currentActivity setEligibleForSearch:NO]; // Defaults to NO
[currentActivity setEligibleForPublicIndexing:NO]; // Defaults to NO

// Set this activity as the current user activity
// Only one activity may be current at a time on a device. Calling this method invalidates any other current activities.
[currentActivity becomeCurrent];

After this, the activity above should be available for Handoff (although more work is required to properly handle the "Handoff").