【问题标题】:Accessing domainIdentifier from NSUserActivity with Spotlight使用 Spotlight 从 NSUserActivity 访问 domainIdentifier
【发布时间】:2015-12-14 22:38:59
【问题描述】:

我将“Person”和“Product”对象索引到 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")
    }
}

你可以看到我正在使用 domainIdentifiers: "person" & "product"。但是当我回到应用程序时,我将如何访问这些domainIdentifiers?

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
}

【问题讨论】:

    标签: ios swift corespotlight nsuseractivity


    【解决方案1】:

    据我所知,在CoreSpotlight 中,您无法直接访问domainIdentifier。您所拥有的是uniqueIdentifier,因此您可以使用某种前缀来解决它。要获取标识符,您可以使用:

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

    在你的AppDelegate

    【讨论】:

    • 谢谢。每个 ProductPerson 都有一个关联的网站 URL - 即 example.com/person/1234 example.com/product/1234。所以对我来说使用 url 字符串作为唯一标识符可能是有意义的,然后在 continueUserActivity 中解析它。
    • 也许不是整个 URL,但 product/1234, person/1234 似乎是一个想法,易于解析。
    猜你喜欢
    • 1970-01-01
    • 2016-05-08
    • 2018-12-31
    • 2016-10-14
    • 1970-01-01
    • 1970-01-01
    • 2015-11-15
    • 2022-08-22
    • 1970-01-01
    相关资源
    最近更新 更多