Contents

CSImportExtension

An object that provides searchable attributes for file types that the app supports.

Declaration

class CSImportExtension

Overview

To create a Spotlight File Import extension, add a target to your app using the Spotlight File Import extension template in Xcode. The template project contains a subclass of CSImportExtension. To index content on a user’s device, Core Spotlight loads your extension and invokes the update(_:forFileAt:) method. Core Spotlight passes a CSSearchableItemAttributeSet and URL of a file to the extension, and you set properties that are relevant for the file.

Typically, your extension loads details about the file and uses that information to set properties of the attribute set. For example, if your app contains files that are notes the user creates, it does the following:

class NoteImporter: CSImportExtension {
    override func update(_ attributes: CSSearchableItemAttributeSet, forFileAt contentURL: URL) throws {
        // NoteDetails is an app-defined object that encapsulates a note.
        guard let note = NoteDetails.noteDetails(for: contentURL) else {
            throw NoteError.noteNotFound
        }
        attributes.title = note.title
        attributes.contentCreationDate = note.creationDate
        attributes.userCreated = NSNumber(booleanLiteral: true)
    }
}

To specify the file types your app supports, set the value of CSSupportedContentTypes in your extension’s Info.plist file to an array of file type identifiers. For more information about file type identifiers, see Uniform Type Identifiers. The app in the previous example configures the extension’s Info.plist as follows:

<key>NSExtension</key>
<dict>
    <key>NSExtensionPointIdentifier</key>
    <string>com.apple.spotlight.import</string>
    <key>NSExtensionPrincipalClass</key>
    <string>FileImporter</string>
    <key>NSExtensionAttributes</key>
    <dict>
        <key>CSSupportedContentTypes</key>
        <array>
            <string>com.example.mynoteapp.note</string>
        </array>
    </dict>
</dict>

Topics

Providing searchable attributes

See Also

Spotlight app extensions