Index and Search PDFs Using iOS Spotlight

PSPDFKit's PDFLibrary implements a full-text-search engine based on a SQLite database that can also optionally index documents with Spotlight, so the user can search for documents (and their text) from the native device search. To enable this, set up PDFLibrary, and set the spotlightIndexingType(opens in a new tab) property before calling PDFLibrary.updateIndex(completionHandler:)(opens in a new tab). This can be set to one of the following:

Retrieving Documents from Spotlight

When the user taps on a searchable item from your app in Spotlight search results, your app delegate’s application(_:continue:restorationHandler:) method is called. In your implementation of this method, call PDFLibrary.fetchSpotlightIndexedDocument(for:completionHandler:)(opens in a new tab) to retrieve the document, if any:

func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {
guard let library = PSPDFKit.SDK.shared.library else {
logError("Unable to get shared PDFLibrary instance to continue user activity.")
return false
}
library.fetchSpotlightIndexedDocument(for: userActivity) {
guard let document = $0 else { return }
// Open the document in a `PDFViewController`.
}
return true
}