1

Docs on updateUserActivityState are a bit vague to me.

https://developer.apple.com/documentation/uikit/uiresponder/1621095-updateuseractivitystate

Subclasses override this method to update the state of the given user activity. You should add state representing the user's activity into the NSUserActivity object using its addUserInfoEntries(from:) method.

When the state is dirty, you should set the needsSave property of the NSUserActivity to true, and this method will be called at an appropriate time.

So this method is called when we do:

open override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    self.userActivity?.needsSave = true
}

And we can then update the activity accordingly:

override func updateUserActivityState(activity: NSUserActivity) {

  activity.addUserInfoEntriesFromDictionary(someInfo)
  super.updateUserActivityState(activity)
}

But when we only set the activity once and never update it, should we also always have to implement updateUserActivityState?

For example when setting the activity once in viewDidLoad:

override open func viewDidLoad() {
    super.viewDidLoad()

    let activity = NSUserActivity(activityType: "com.myApp")
    activity.title = "Viewing"
    activity.userInfo = ["myApp.item.key": ["Some", "Things"]]
    self.userActivity = activity
    self.userActivity?.becomeCurrent()
}
Community
  • 1
  • 1
Tieme
  • 62,602
  • 20
  • 102
  • 156

1 Answers1

0

But when we only set the activity once and never update it, should we also always have to implement updateUserActivityState?

If the NSUserActivity object never changes, and is fully configured when created, there is no need to override the UIResponder.updateUserActivityState method.

However, for consistency and future-proofing (in the event you ever do make that user activity object mutable), the app might still want to use updateUserActivityState to fill in the activity when the responder chain is notified of changes.

Blazej SLEBODA
  • 8,936
  • 7
  • 53
  • 93