Ios10 swift 3核心聚光灯搜索无法获得nsuseractivity的标题

时间:2016-11-04 16:52:41

标签: ios swift3 ios10 corespotlight nsuseractivity

我没有在userActivity.title上获得任何内容,它在appdelegate处变为零。检查下面,这曾经在ios9中工作,我记得。我需要在attributeSet.title中获取Title集,但它始终为nil。

索引我的项目的代码

    let attributeSet = CSSearchableItemAttributeSet(itemContentType: kUTTypeText as String)
    attributeSet.title = "ALFRESCO" //restnombre //project[0]
    attributeSet.contentDescription = "\(tipocomida)"

    let item = CSSearchableItem(uniqueIdentifier: "\(restid)", domainIdentifier: "com.cgcompany.restaurants", attributeSet: attributeSet)
    CSSearchableIndex.default().indexSearchableItems([item]) { (error: Error?) -> Void in
        if let error = error {
            print("Indexing error: \(error.localizedDescription)")
        } else {
            print("Search item successfully indexed!")
        }
    }

app delegate中的代码

 func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {
      //  if #available(iOS 9.0, *) {

            print("CALLING CONTINUE USER ACT")

        if userActivity.activityType == CSSearchableItemActionType {
                print("ACT TYPE IS CSSEARCHABLEITEM")

      var title = "NOTITLE"
            if (userActivity.title) != nil {
                title = userActivity.title!
            }


                if let uniqueIdentifier = userActivity.userInfo?[CSSearchableItemActivityIdentifier] as? String {
                 print ("Calling FROM SPOTLIGHTSEARCH")


                    let rootViewController = self.window!.rootViewController as! UINavigationController
                    let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

                    let restaurantdetail = mainStoryboard.instantiateViewController(withIdentifier: "restaurant_detail2") as! VCviendorest
                    let detailurl = "http://www.urltoget.com/iphone/\(uniqueIdentifier)/fromiOSSearch.html"

                    restaurantdetail.IDREST = uniqueIdentifier
                    restaurantdetail.NOMBRERESTAURANT = "\(title)" //userActivity.title!

                    restaurantdetail.DETAILURL = detailurl
                    rootViewController.pushViewController(restaurantdetail, animated: true)

                }
            }
     //   } else {
            // Fallback on earlier versions
       // }

        return true
    }

1 个答案:

答案 0 :(得分:2)

当我的应用程序在iOS 10设备上运行时(在XCode 7.1上),我遇到了与您相同的问题,经过几次测试,我需要将NSUserActivity与CSSearchableItemAttributeSet结合使用,并停止使用CSSearchableIndex的CSSearchableItem和indexSearchableItems方法。这是代码:

// Config the attributeSet
let attributeSet = CSSearchableItemAttributeSet(itemContentType: kUTTypeText as String)
attributeSet.title = "ALFRESCO" //restnombre //project[0]
attributeSet.contentDescription = "\(tipocomida)"
attributeSet.identifier = "\(restid)"

if #available(iOS 10, *) {
    // Init user activity combining with the attribute set
    let bundleId = NSBundle.mainBundle().bundleIdentifier ?? ""
    let activity = NSUserActivity(activityType: "\(bundleId).spotlight")
    activity.userInfo = [CSSearchableItemActivityIdentifier: attributeSet.identifier!]
    activity.title = attributeSet.title
    activity.eligibleForHandoff = false
    activity.eligibleForSearch = true
    activity.eligibleForPublicIndexing = true
    activity.contentAttributeSet = attributeSet
    activity.becomeCurrent()
} else {
    // continue using CSSearchableItem with your old code
    // let item = CSSearchableItem(...
}

在您的应用委托上,您需要将支票更新为:

let activityType = (NSBundle.mainBundle().bundleIdentifier ?? "") + ".spotlight"
if userActivity.activityType == CSSearchableItemActionType || userActivity.activityType == activityType {
    // here for your code
}