Search code examples
iossearchmoviecorespotlightnsuseractivity

How to change year date in CSSearchableItem (content type — kUTTypeMovie)?


I have an app with video content and I track all viewed items with NSUserActivity to add it to Spotlight. The result looks perfect, except year date, it is always a year when NSUserActivity was added to the index. But I need to change this to release year of a movie.

Movie item representation in Spotlight

let activity = NSUserActivity(activityType: "com.bundleid.movie")
activity.isEligibleForSearch = true
activity.title = "Mess"
let movie = kUTTypeMovie as String
let attributes = CSSearchableItemAttributeSet(itemContentType: movie)
attributes.title = "Mess"
attributes.contentDescription = "Even if you get behind bars..."
attributes.thumbnailData = try? Data(contentsOf: thumbURL) // cover
attributes.rating = NSNumber(value: 3.7)
attributes.ratingDescription = "(Kinopoisk: 7.9, IMDb: 7.4)"
attributes.duration = NSNumber(value: 5785) // 1:36:25 in sec
attributes.contentCreationDate = dateFormatter.date(from: "1989") // ¯\_(ツ)_/¯
activity.contentAttributeSet = attributes

I'm trying setting not only attributes.contentCreationDate but also addedDate and all other Dates available in CSSearchableItemAttributeSet_Media.h. But it is always 2018(


Solution

  • I was able to reproduce this.

    However, when I put this CSSearchableItemAttributeSet not inside of a NSUserActivity but instead inside a CSSearchableItem and index that with CSSearchableIndex.default().indexSearchableItems(), the search result says 1989 as desired.

    Would that work for you?