RehcsifMit
RehcsifMit

Reputation: 270

Can't get CoreSpotlight to work properly on the Mac... sample code anyone?

Can someone point me to some sample code for using CoreSpotlight on the Mac? I can’t find a single sample code example (for mac, plenty of iOS ones out there). I have already successfully implemented CS on our iOS app.

Using code similar to what I used for iOS, I’m getting it to index the content, but it’s very minimal: it gives me the generic app icon for the content (rather than the custom thumbnail image I specify) and has no description, etc (on the Spotlight pane, the entire right side is blank).

Also, it's unclear to me what code I need to add to AppDelegate to handle the click on the spotlight data. I used `func application(_ application: NSApplication, continue userActivity: NSUserActivity, restorationHandler:) on iOS but this doesn't seem to get called on the Mac.

Here's the code I'm using to index the content:

        attributeSet.title = content.name
        attributeSet.contentDescription = content.description
        if let image = image {
            let cgImage = image.cgImage(forProposedRect: nil, context: nil, hints: nil)!
            let bitmapRep = NSBitmapImageRep(cgImage: cgImage)
            attributeSet.thumbnailData = bitmapRep.representation(using: NSBitmapImageRep.FileType.jpeg, properties: [:])!
        }
        let item = CSSearchableItem(uniqueIdentifier:content.uuid, domainIdentifier: "com.my.app.bundle.id", attributeSet: attributeSet)
        searchableIndex?.indexSearchableItems([item]) { error in
            if let error = error {
                NSLog("CoreSpotlight Indexing error: \(error.localizedDescription)")
            }
        }

And this is an example of what I get in Spotlight: spotlight screen shot

A bit of sample code would clear this right up, Methinks.

Thanks in advance.

Upvotes: 0

Views: 213

Answers (0)

Related Questions