您需要确保staleDate属性的值在超时时间之前设置。下面是一个示例,其中staleDate为当前时间加上5秒:
let activity = NSUserActivity(activityType: "com.example.app")
activity.title = "Example Activity"
activity.userInfo = ["exampleKey": "exampleValue"]
activity.requiredUserInfoKeys = ["exampleKey"]
activity.isEligibleForSearch = true
activity.isEligibleForPublicIndexing = true
activity.isEligibleForHandoff = true
activity.isEligibleForPrediction = true
activity.persistentIdentifier = NSUserActivityPersistentIdentifier("com.example.app.activity")
activity.expirationDate = Date().addingTimeInterval(60 * 60 * 24) // expire 24 hours from now
activity.keywords = ["example", "activity"]
activity.becomeCurrent()
// Set staleDate to 5 seconds from now
activity.staleDate = Date().addingTimeInterval(5)
注意:如果将staleDate设置为早于当前时间,则该属性将无效,因为活动已经过期,即使它的过期时间尚未到达。