簡體   English   中英

使用Spotlight從NSUserActivity訪問domainIdentifier

[英]Accessing domainIdentifier from NSUserActivity with Spotlight

我將“人物”和“產品”對象索引到Spotlight中,如下所示:

// 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")
    }
}

您可以看到我正在使用domainIdentifier :“人”和“產品”。 但是,當我回到應用程序中時,如何訪問這些domainIdentifier呢?

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
}

據我所知,在CoreSpotlight您沒有直接訪問domainIdentifier 您擁有的是uniqueIdentifier ,因此您可以在其中使用某種前綴解決它。 要獲取標識符,您可以使用:

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

}

在您的AppDelegate

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM