我已经实现了苹果在WWDC24新推出的
ShowInAppSearchResultsIntent
和AppShortcutsProvider
。
import AppIntents
import Foundation
import UIKit
@AssistantIntent(schema: .system.search)
struct CineverseSearchIntent: ShowInAppSearchResultsIntent {
// static var title: LocalizedStringResource = "Search in Cineverse for"
static var searchScopes: [StringSearchScope] = [.general]
@Parameter(requestValueDialog: IntentDialog("What would you like to search for?"))
var criteria: StringSearchCriteria
@MainActor
func perform() async throws -> some IntentResult {
let searchString = criteria.term
print("Searching for \(searchString)")
msgWithSingleButton(message: "Searching for \(searchString)", title: "Siri", buttonTitle: "OK")
return .result()
}
}
class AppShortcuts: AppShortcutsProvider {
static var appShortcuts: [AppShortcut] {
AppShortcut(
intent: CineverseSearchIntent(),
phrases: [
"using \(.applicationName) search for",
"search on \(.applicationName) app"
],
shortTitle: "Search Movie",
systemImageName: "sparkles"
)
}
}
//Alert
func msgWithSingleButton(message: String, title: String = "",buttonTitle : String){
let alertView = UIAlertController(title: title, message: message, preferredStyle: .alert)
alertView.addAction(UIAlertAction(title: buttonTitle, style: .default, handler: nil))
let scenes = UIApplication.shared.connectedScenes
let windowScene = scenes.first as? UIWindowScene
let window = windowScene?.windows.first
window?.rootViewController?.present(alertView, animated: true)
}
我已经尝试过上面的代码。它正在触发模拟器(iOS 18.0)中的条件。 Siri 正在说出这句话 - “您想搜索什么?” - 我已经使用
@Parameter
实现了,但之后它就不起作用了。
但在真实设备中,它是直接打开应用程序,而不是调用条件。并在控制台中显示错误Failed to generate TargetContentIdentifier for criteria
。
官方文档网址 Apple 开发者
这是我实现的完整代码。请找到解决方案。
要修复 ShowInAppSearchResultsIntent 实现,请尝试以下操作:
@AssistantIntent(intent: CineverseSearchIntent.self)
struct CineverseSearchIntent: ShowInAppSearchResultsIntent {
static var title: LocalizedStringResource = "Search in Cineverse"
@Parameter(title: "Search Term")
var criteria: String
@MainActor
func perform() async throws -> some IntentResult {
print("Searching for \(criteria)")
// Your search logic here
return .result()
}
}
这应该适用于模拟器和设备。您看到的错误应该通过这些更改来解决。