Ios10 swift 3-ядерный поиск в центре внимания не может получить название nsuseractivity

я ничего не получаю в userActivity.title , в appdelegate он становится нулевым. проверьте ниже, это работало в ios9, насколько я помню. мне нужно установить заголовок в attributeSet.title , но он всегда равен нулю.

Код для индексации моего товара

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

Код в делегате приложения

 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
    }

person lorenzo gonzalez    schedule 04.11.2016    source источник


Ответы (1)


У меня была та же проблема, что и у вас, когда мое приложение работало на устройстве iOS 10 (на XCode 7.1), после нескольких тестов мне нужно объединить NSUserActivity с CSSearchableItemAttributeSet и прекратить использование методов CSSearchableItem и indexSearchableItems CSSearchableIndex. Вот код:

// 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
}
person bubuxu    schedule 06.12.2016