Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I Write a Spotlight Importer in Swift?

I need to write a Spotlight Importer for an application that I've written in Swift, and am referring to the official Apple guide for Writing a Spotlight Importer.

It seems straightforward enough, however creating a Spotlight Importer project creates a default setup for an Objective-C implementation. Now, working with Objective-C isn't a huge problem (I've used it plenty of times in the past) but everything I've written for my application is in Swift, so I'd really I'd like to write the importer in Swift too to avoid switching between languages, and also so I can share some of the code that I've already done for reading/writing files.

Firstly, is it possible to write a Spotlight Importer using Swift instead of Objective-C? And if it is, where should I start (e.g- if I take the Objective-C starting point, what would I do to switch over to Swift instead)?

like image 929
Haravikk Avatar asked Nov 10 '22 08:11

Haravikk


1 Answers

It took me a bit of time to get this to work.

Instead of adding Swift code to the mdimporter, I import an embedded framework already setup for my app.

I removed all the example code except main.c and GetMetadataForFile.m. In the latter I import my framework where all the functionality now resides as Swift code.

The built mdimporter is added to the app. In the File Inspector set Location to Relative to Build Products.

The app then adds the mdimporter with a Copy Files Build Phase.

  • Destination: Wrapper
  • Subpath: Contents/Library/Spotlight

The following needs to be added to the Run Search Paths build setting, as we are linking to the app's embedded frameworks.

@loader_path/../../../../../Frameworks

If you get compiler error that the framework module can't be found when building the app, depending on how your workspace is set up, you might need to modify your app's Scheme.

  • Turn off Parallelize Build
  • Add the Build targets in this sequence:
    1. Frameworks project(s)
    2. mdimporter project
    3. App project

The additional benefit of having all the logic in a framework, is that it can be prototyped and verified in a Playground. A million times easier than debugging an mdimporter plugin.

like image 96
Richard Kruse Nees Avatar answered Nov 15 '22 04:11

Richard Kruse Nees