Sub topic 12
For developers: adopting Handoff with NSUserActivity
This chapter shows how an app opts in to Handoff. It assumes you know basic Swift, have built a simple iOS or SwiftUI app, and understand from earlier chapters what Handoff does for the user: you start something on one device and continue it on another.
The working model
Handoff in an app has two halves.
- The sending app describes what the user is doing with an
NSUserActivityobject. The object holds an activity type, a title, and a smalluserInfodictionary. The app marks it eligible for Handoff and makes it current. - The receiving app declares that it handles that activity type. When the user chooses the Handoff icon, the system delivers the activity, and the app reads
userInfoand shows the matching screen.
The system moves only this small description. It does not move your documents or your database. If the receiving device needs the full note, the app must load it by the identifier it received, for example from iCloud or from your own server.
Both devices must run the app (or apps that share a team identifier and activity type), be signed in to the same Apple Account, and meet the requirements from earlier chapters.
Step 1: Advertise the activity
Create the activity when the user starts the task, and keep it current while they stay on it. The example below does this in a view controller.
import UIKit
final class NoteViewController: UIViewController {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let activity = NSUserActivity(activityType: "com.example.notes.viewing-note")
activity.title = "Viewing a note"
activity.userInfo = ["noteID": "42"]
activity.isEligibleForHandoff = true
userActivity = activity
activity.becomeCurrent()
}
}Each line has a job:
- The activity type is a reverse-DNS string you choose. It must match on both sides.
userInfocarries the minimum state needed to resume, here a note identifier.isEligibleForHandoffallows the system to advertise the activity.- Assigning
userActivityon the view controller lets UIKit manage the activity's lifetime and callbecomeCurrent()andinvalidate()as the screen comes and goes. CallingbecomeCurrent()yourself, as shown, makes the intent explicit.
Step 2: Declare the type in Info.plist
The receiving app must list the activity type under the NSUserActivityTypes key in its Info.plist. Without this entry the system does not deliver the activity, even if the code is correct.
<key>NSUserActivityTypes</key>
<array>
<string>com.example.notes.viewing-note</string>
</array>Step 3: Receive the activity
In SwiftUI, attach onContinueUserActivity to a view and give it the same activity type.
import SwiftUI
@main
struct NotesApp: App {
var body: some Scene {
WindowGroup {
ContentView()
.onContinueUserActivity("com.example.notes.viewing-note") { activity in
if let id = activity.userInfo?["noteID"] as? String {
print("Continue note \(id)")
}
}
}
}
}Replace the print call with navigation to the note. A real app would look up the note by id and select it in its navigation state.
Common errors
| Symptom | Likely cause |
|---|---|
| The Handoff icon never appears | `isEligibleForHandoff` is false, or `becomeCurrent()` was never called |
| The icon appears but the app does not open to the right screen | The activity type is missing from `NSUserActivityTypes`, or the strings differ by one character |
| The app opens but the state is empty | The code reads a key that the sender did not put in `userInfo` |
| Works on one pair of devices only | The devices are not signed in to the same Apple Account, or the apps use different team identifiers |
Keep userInfo small. Apple limits the payload size, so send identifiers rather than content. Store only values that are property-list types, such as strings, numbers, dates, and arrays or dictionaries of those.
Testing
Test on two physical devices that are signed in to the same Apple Account, with Bluetooth and Wi-Fi on and Handoff enabled. The Simulator does not support Handoff between devices. Build the app on both devices, open the sending screen, and look for the app icon in the Mac Dock or the iPhone App Switcher.
Exercise
Your app advertises an activity and the Handoff icon appears on the Mac, but clicking it opens the app without showing the note. The sender code sets isEligibleForHandoff = true and calls becomeCurrent(). Name two things to check on the receiving side.
Show answer
First, confirm the activity type string is listed under NSUserActivityTypes in the receiving app's Info.plist and matches the sender's string exactly. Second, confirm the handler (for example onContinueUserActivity with the same type) reads the correct key from userInfo, such as "noteID", and navigates to that note.
Exercise
Why should userInfo hold a note identifier instead of the note's full text?
Show answer
The system sends only a small description of the activity, and the payload size is limited. An identifier keeps the payload small and avoids sending personal data through the activity. The receiving app loads the full note from iCloud or its own server.