gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 19:59:45 27 4
gpt4 key购买 nike

我在 userActivity.title 上没有得到任何东西,它在 appdelegate 处变为 nil。检查下面,这曾经在 ios9 中工作,我记得。我需要在 attributeSet.title 中设置 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!")
}
}

应用程序委托(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
}

最佳答案

当我的应用程序在 iOS 10 设备(在 XCode 7.1 上)上运行时,我遇到了与您相同的问题,经过几次测试,我需要将 NSUserActivity 与 CSSearchableItemAttributeSet 结合起来,并停止使用 CSSearchableItem 的 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(...
}

在您的应用委托(delegate)上,您需要将检查更新为:
let activityType = (NSBundle.mainBundle().bundleIdentifier ?? "") + ".spotlight"
if userActivity.activityType == CSSearchableItemActionType || userActivity.activityType == activityType {
// here for your code
}

关于ios10 swift 3核心聚光灯搜索无法获得nsuseractivity的标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40427984/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com