Contents

NSUserActivity

A representation of the state of your app at a moment in time.

Declaration

class NSUserActivity

Mentioned in

Overview

The NSUserActivity class is a lightweight type that encapsulates the state of an activity you perform in your app. When someone performs a relevant action in your app, create an instance of this class to capture that activity. The system uses the user activity objects you provide to facilitiate features like Handoff and Quick Notes. For example, Handoff sends your user activity object to the person’s other devices so your app can replicate the activity there.

Create an NSUserActivity object and initialize it with the activityType string for that particular activity. You define the activity types your app supports and create reverse-DNS strings for each one in your code. Typically, activities correspond to actions that a person takes in your app’s interface. For example, you might create one activity for viewing items and a separate activity for completing edits to an item.

Add enough information to your user activity object to recreate the activity in your app at a later time. If your activity has an associated URL, place it in the webpageURL property. If your user activity object refers to a specific piece of your app’s content, specify the identifier for that content using the targetContentIdentifier, appEntityIdentifier, or externalMediaContentIdentifier property. Place any other app-specific data in the userInfo dictionary. Provide a human-readable title for the activity, and enable the features your activity supports such as Handoff and Spotlight indexing. Fill in other properties as needed for your specific activity type.

Create user activity objects in response to specific interactions with your app’s interface. When someone performs a significant task in your interface, create a user activity object and call its becomeCurrent() method to make it your app’s current activity. For example, you might do this when someone opens a new document in your app. Handoff and other features operate on the current activity and use it as context for their behavior. In the case of opening a document, Handoff indicates that the person can open that document on their other devices. When the person stops the activity in your interface, or the activity is no longer relevant, call the resignCurrent() or invalidate() method, or create a new user activity object and make it the current one.

For features like Handoff to work, the system needs to know which types of activities it can deliver to your app. To specify the activity types you support, add the NSUserActivityTypes key to your app’s Info.plist file. Configure this key using the Info tab of your Xcode project, and set its value to an array of strings. For each string, specify one of the activity types you use to create your NSUserActivity objects. You can specify all of your app’s activity types or only a subset.

System features like Siri and Apple Intelligence use contextual information from your app’s interface to improve their responses. Assign AppEntity types directly to your views when possible, but you can also assign a user activity object to your view as needed. In SwiftUI, create and configure this user activity object using the userActivity(_:element:_:) modifier. In UIKit and AppKit, assign the user activity object to the userActivity property of a responder object in your interface. Use the appEntityIdentifier property of your user activity object to provide the entity for your view.

Topics

Creating a user activity object

Monitoring activity-related behaviors

Describing the activity

Enabling system behaviors

Specifying app identifiers

Browsing the web

Specifying activity-related data

Accessing feature-specific data

Registering and invalidating activities

Managing type-safe access to user info

Working with continuation streams

Providing SiriKit with activity details

Reporting errors

Deprecated

Default Implementations

See Also

Host App Interaction