iosswiftcorespotlightnsuseractivity

Accessing domainIdentifier from NSUserActivity with Spotlight


I'm indexing 'Person' and 'Product' objects into Spotlight like so:

// Person
let personItem = CSSearchableItem(uniqueIdentifier: personID, domainIdentifier: "person", attributeSet: attributeSet)                

CSSearchableIndex.defaultSearchableIndex().indexSearchableItems([personItem]) { (error: NSError?) -> Void in
    if let error = error {
        print("Indexing error: \(error.localizedDescription)")
    } else {
        print("person added to spotlight")
    }
}


// Product
let productItem = CSSearchableItem(uniqueIdentifier: productID, domainIdentifier: "product", attributeSet: attributeSet)                

CSSearchableIndex.defaultSearchableIndex().indexSearchableItems([productItem]) { (error: NSError?) -> Void in
    if let error = error {
        print("Indexing error: \(error.localizedDescription)")
    } else {
        print("product added to spotlight")
    }
}

You can see that I'm using to domainIdentifiers: "person" & "product". But how would I access these domainIdentifiers when I come back into the app?

func application(application: UIApplication, continueUserActivity userActivity: NSUserActivity, restorationHandler: ([AnyObject]?) -> Void) -> Bool {
    if userActivity.activityType == CSSearchableItemActionType {

        // if product do this
        // if person do that

    }

    return true
}

Solution

  • From what I know, in CoreSpotlight you don't have direct access to domainIdentifier. What you have is uniqueIdentifier, so you can work around it with some prefix in it of some sort. To get the identifier you can use:

    if let itemActivityIdentifier = userActivity.userInfo?["kCSSearchableItemActivityIdentifier"] {
    
    }
    

    in your AppDelegate.