How to debug a Spotlight extension in iOS
Asked Answered
B

2

5

I have created a Spotlight extension in iOS and want to debug it. However, since the Spotlight index is controlled by iOS I don't know which app will trigger an indexing run. I tried Safari but to no avail.

Bounder answered 11/8, 2016 at 23:24 Comment(0)
M
6

Select the Spotlight extension in the scheme selector in the top left of the Xcode window.

Xcode scheme selector

When you click on run, Xcode will ask you which app you want to run. Select your extension's parent app. This will run your app as normal but you will now be attached to the extension for debugging.

The next step is to trigger a Spotlight reindex. Do this from your device's settings page (it must be enabled for development from Xcode):

Settings -> Developer -> CoreSpotlight testing

Settings -> Developer CoreSpotlight testing

Here you can select Reindex All Items or Reindex All Items With Identifiers. These two options correspond to the Core Spotlight IndexRequestHandler: CSIndexExtensionRequestHandler methods below:

override func searchableIndex(_ searchableIndex: CSSearchableIndex, reindexAllSearchableItemsWithAcknowledgementHandler acknowledgementHandler: @escaping () -> Void) {
    // Reindex all data with the provided index
}

override func searchableIndex(_ searchableIndex: CSSearchableIndex, reindexSearchableItemsWithIdentifiers identifiers: [String], acknowledgementHandler: @escaping () -> Void) {
    // Reindex any items with the given identifiers
}

You will now be able to debug these methods as normal, with runtime errors displayed in the source editor.

Morry answered 9/3, 2019 at 13:3 Comment(4)
I couldn't get this to work, I tried putting some breakpoints or print statements in the two functions, but couldn't get it to get called when I press the buttons in Settings->Developer.Gaitan
@PhuahYeeKeat Did you select the extension in the top left panel of Xcode instead of the main app? Also, did you add the extension using the Spotlight Extension option in the add new target window?Morry
I realised it can't seems to work using a simulator, tried on a real device and it worked! Thanks!Gaitan
@PhuahYeeKeat I’ll have a look - I’m not good with Core Data!Morry
A
1

You can choose it at top left corner of Xcode. enter image description here

Aldwon answered 12/8, 2016 at 3:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.