NSUserActivity
An NSUserActivity object provides a lightweight way to capture app state so that it can be restored later. NSUserActivity supports activity restoration from Handoff, which lets users begin an activity on one device and continue it on another, and from search results. User activities include actions such as viewing app content, editing a document, viewing a web page, or watching a video. You can use the properties described in Specifying Eligibility to set whether an activity should be eligible for Handoff or different types of search (activities are eligible for Handoff by default).
Using Handoff, for example, a user who is editing a document on a Mac can continue editing that document on an iPhone. A person watching a movie on an iPad can continue watching on a Mac. In both cases, the users resume the activity at the point at which they stopped on the original device. Your apps should work to make this experience as seamless as possible.
In iOS 9 and later, an NSUserActivity object can be indexed and made available to users in search results when they perform on-device searches in Spotlight. To ensure that users get rich search results, you can provide metadata that fully describes the activity. When users select your activity from a list of search results, the system passes that activity and result to your app as it is launched, so that you can restore app state appropriately. Note that NSUserActivity is not intended to help you index arbitrary data within your app; to index app content, use the APIs of the Core Spotlight framework (to learn more, see Core Spotlight Framework Reference).
A user activity may be continued only in an app that (1) has the same developer Team ID as the activity's source app and (2) supports the activity's type. The app specifies the activity types it supports in its Info.plist under the NSUserActivityTypes key. When receiving a user activity for continuation, the system locates the appropriate app to launch by finding apps with the same Team ID as the sending app, then filtering on the incoming activity's type.
To have a Team ID, apps must be distributed through the App Store or be Developer ID–signed. Apps sharing a Team ID can selectively route activities between each other through judicious use of the activity activityType property. This lends support to related apps that are structured differently between the platforms. For example, a single Mac app can route each of its activity types to smaller, special-purpose apps on iOS.
-
Initializes and returns a newly created
NSUserActivityobject with the given activity type.Declaration
Swift
init(activityTypeactivityType: String)Objective-C
- (instancetype)initWithActivityType:(NSString *)activityTypeParameters
activityTypeThe type of activity to be continued. The value is a developer-defined string in reverse-DNS format by convention, for example,
com.myCompany.myEditor.editing.Return Value
An
NSUserActivityobject.Availability
Available in iOS 8.0 and later.
-
Initializes and returns a newly created
NSUserActivityobject with the first activity type from theNSUserActivitykey in the app’sInfo.plist.Declaration
Swift
init()Objective-C
- (instancetype)initReturn Value
An
NSUserActivityobject.Availability
Available in iOS 8.0 and later.
-
A Boolean value that indicates if the activity can be publicly accessed by all iOS users.
Declaration
Swift
var eligibleForPublicIndexing: BoolObjective-C
@property(getter=isEligibleForPublicIndexing) BOOL eligibleForPublicIndexingDiscussion
The default value of this property is
NOfalse, which means that the activity contains private or sensitive information or that the activity would not be useful to other users. If you set this property toYEStrue, the activity must also have values forrequiredUserInfoKeysorwebpageURL.In iOS 9.0, activities marked as
eligibleForPublicIndexingare added to the private, on-device index. Identifying an activity as public confers an advantage when you also add web markup to the content on your related website. Specifically, when users engage with your app’s public activities in search results, it indicates to Apple that public information on your website is popular, which can help increase your ranking and potentially lead to expanded indexing of your website’s content.Availability
Available in iOS 9.0 and later.
-
eligibleForSearch eligibleForSearchPropertyA Boolean value that indicates if the activity should be added to the on-device index.
Declaration
Swift
var eligibleForSearch: BoolObjective-C
@property(getter=isEligibleForSearch) BOOL eligibleForSearchDiscussion
The default value of this property is
NOfalse.Availability
Available in iOS 9.0 and later.
-
eligibleForHandoff eligibleForHandoffPropertyA Boolean value that indicates if the activity can be continued on another device using Handoff.
Declaration
Swift
var eligibleForHandoff: BoolObjective-C
@property(getter=isEligibleForHandoff) BOOL eligibleForHandoffDiscussion
The default value of this property is
YEStrue.Availability
Available in iOS 9.0 and later.
-
expirationDate expirationDatePropertyThe date after which the activity is no longer eligible to be indexed or handed off.
Declaration
Swift
@NSCopying var expirationDate: NSDateObjective-C
@property(copy) NSDate *expirationDateDiscussion
If you don’t set the
expirationDateproperty appropriately, the system automatically expires the activity after a period of time.Availability
Available in iOS 9.0 and later.
-
A Boolean value that determines whether the continuing app can request streams to be opened back to the originating app.
Declaration
Swift
var supportsContinuationStreams: BoolObjective-C
@property BOOL supportsContinuationStreamsDiscussion
If the value of this property is
YEStrue, the continuing app can connect back to the originating app for more information using streams. This value is set toNOfalseby default. It can dynamically be set toYEStrueto selectively support continuation streams based on the state of the user activity.Availability
Available in iOS 8.0 and later.
-
Requests streams back to the originating app.
Declaration
Swift
func getContinuationStreamsWithCompletionHandler(_completionHandler: (NSInputStream?, NSOutputStream?, NSError?) -> Void)Objective-C
- (void)getContinuationStreamsWithCompletionHandler:(void (^)(NSInputStream *inputStream, NSOutputStream *outputStream, NSError *error))completionHandlerParameters
completionHandlerThe completion handler block that returns streams.
The block takes three arguments:
inputStreamThe stream from which the continuing app can read data written by the originating app.
outputStreamThe stream to which the continuing app writes data to be read by the originating app.
errorIf successful,
nil; if not successful, anNSErrorobject that encapsulates the reason why the streams could not be created.Discussion
When an app is launched for a continuation event, it can request streams back to the originating app. Streams can be successfully retrieved only from the
NSUserActivityobject in theNSApplicationorUIApplicationdelegate that is called for a continuation event. The streams are provided by the completion handler in an unopened state, and the delegate should open them immediately to start communicating with the continuing side.Continuation streams are an optional feature of Handoff, and most user activities do not need them for successful continuation. When streams are needed, a simple request from the continuing app accompanied by a response from the originating app is enough for most continuation events.
Availability
Available in iOS 8.0 and later.
-
Dictionary containing app-specific state information needed to continue an activity on another device.
Declaration
Objective-C
@property(copy) NSDictionary *userInfoDiscussion
Each key and value must be of the following types:
NSArray,NSData,NSDate,NSDictionary,NSNull,NSNumber,NSSet,NSString, orNSURL. The system may translate file scheme URLs that refer to iCloud documents to valid file URLs on a continuing device.Availability
Available in iOS 8.0 and later.
-
Adds to the
userInfodictionary the entries from the given dictionary.Declaration
Objective-C
- (void)addUserInfoEntriesFromDictionary:(NSDictionary *)otherDictionaryParameters
otherDictionaryThe dictionary containing entries to be added.
Discussion
Transfer as small a payload as possible in the
userInfodictionary. The more payload data you deliver, the longer it takes the activity to resume.Availability
Available in iOS 8.0 and later.
-
Marks the activity as the activity currently in use by the user.
Declaration
Swift
func becomeCurrent()Objective-C
- (void)becomeCurrentDiscussion
A typical example of a current user activity is the activity associated with the foremost active window. A newly created user activity is eligible for continuation on another device after the first time it becomes current.
Availability
Available in iOS 8.0 and later.
-
Tells the activity that it should no longer be the activity currently in use by the user.
Declaration
Swift
func resignCurrent()Objective-C
- (void)resignCurrentDiscussion
If the activity is the current activity, calling this method tells it to stop being current and sets the current activity to
nil. A noncurrent activity is no longer eligible for continuation on another device.Availability
Available in iOS 9.0 and later.
-
The user activity object’s delegate.
Declaration
Swift
weak var delegate: NSUserActivityDelegate?Objective-C
@property(weak) id< NSUserActivityDelegate > delegateDiscussion
The user activity delegate is informed when the activity is being saved or continued (see
NSUserActivityDelegate).Availability
Available in iOS 8.0 and later.
-
Indicates that the state of the activity needs to be updated.
Discussion
If
YEStrue, the delegate for this user activity receives auserActivityWillSave:callback before the activity is sent for continuation on another device.Availability
Available in iOS 8.0 and later.
-
Invalidates an activity when it's no longer eligible for continuation.
Declaration
Swift
func invalidate()Objective-C
- (void)invalidateDiscussion
The app invokes this method when the user stops engaging in the activity, such as when the window associated with the activity is closed. An invalid activity cannot become current.
Availability
Available in iOS 8.0 and later.
-
activityType activityTypePropertyThe activity type with which the user activity object was created. (read-only)
Declaration
Swift
var activityType: String { get }Objective-C
@property(readonly, copy) NSString *activityTypeDiscussion
This property is read-only because the activity type cannot be changed after the user activity object is created. It’s a good idea to use a reverse-DNS string for the value of this property so that it uniquely identifies the activity type.
Availability
Available in iOS 8.0 and later.
-
An optional, user-visible title for this activity, such as a document name or web page title.
Discussion
An activity’s title can be used in indexing as well as for display in search results.
Availability
Available in iOS 8.0 and later.
-
contentAttributeSet contentAttributeSetPropertyA set of properties that describe the activity.
Declaration
Swift
@NSCopying var contentAttributeSet: CSSearchableItemAttributeSet?Objective-C
@property(copy) CSSearchableItemAttributeSet *contentAttributeSetDiscussion
A
CSSearchableItemAttributeSetobject encapsulates the set of properties you want to display for a searchable activity.Import Statement
Objective-C
@import CoreSpotlight;Swift
import CoreSpotlightAvailability
Available in iOS 9.0 and later.
-
A set of localized keywords that can help users find the activity in search results.
Declaration
Objective-C
@property(copy) NSSet <NSString *> *keywordsDiscussion
The default value of this property is
nil. The system indexes the keywords you provide.Availability
Available in iOS 9.0 and later.
-
requiredUserInfoKeys requiredUserInfoKeysProperty
-
webpageURL webpageURLPropertyWebpage to load in a browser to continue the activity.
Discussion
When no suitable app is installed on a resuming device and the
webpageURLproperty is set, the specified webpage is loaded and the user activity is continued in a web browser.If your activity's content can be restored on the web or you support Safari universal links, be sure to set this property so that the system can resume the activity in Safari or your app. After setting the
webpageURLproperty on an activity for whicheligibleForSearchisYEStrue, also set therequiredUserInfoKeysproperty, using the keys of theuserInfodictionary that must be stored. If you don’t also set therequiredUserInfoKeysproperty, theuserInfodictionary will be empty when the activity is restored.If
eligibleForSearchisYEStruefor this activity and you’re using bothNSUserActivityand web markup to index the same item, setwebpageURLto the relevant URL on your website to avoid showing duplicate results in Spotlight. TheNSUserActivityAPI does not perform any modifications to the URL that you specify. URL components, such as the query string and the fragment identifier, are used for matching the item against pages that are indexed by Applebot.Availability
Available in iOS 8.0 and later.
Copyright © 2016 Apple Inc. All rights reserved. Terms of Use | Privacy Policy | Updated: 2015-12-17
