iOS - AppDelegate 在启动 Siri Intent 时不检索 userActivity
iOS - AppDelegate not retrieving userActivity when Siri intent is launched
实际上是在 swiftUI 中制作应用程序并尝试处理 Siri 意图以在我的应用程序中开始通话。我在我的项目中创建了一个扩展目标,在主目标和扩展目标中添加了 Siri 功能和应用程序组,我的意图处理程序完全正确地触发了 Siri 请求,启动 Siri 并要求拨打某些联系人的电话完美无缺,我的应用程序启动了此时应该从意图中收到 activity,但什么也没发生...
我的意图处理程序如下所示:
import Intents
class IntentHandler: INExtension, INStartCallIntentHandling {
func handle(intent: INStartCallIntent, completion: @escaping (INStartCallIntentResponse) -> Void) {
let userActivity = NSUserActivity(activityType: NSStringFromClass(INStartCallIntent.self))
guard intent.contacts?.first?.personHandle?.value != nil else {
completion(INStartCallIntentResponse(code: .failureContactNotSupportedByApp, userActivity: userActivity))
return
}
let response = INStartCallIntentResponse(code: .continueInApp, userActivity: userActivity)
completion(response)
}
func resolveContacts(for intent: INStartCallIntent, with completion: @escaping ([INStartCallContactResolutionResult]) -> Void) {
var contactName = ""
if let contacts = intent.contacts {
contactName = contacts.first?.displayName ?? ""
}
//This shared method is used to get contact data for completion
DataManager.sharedManager.findContact(contactName: contactName, with: {
contacts in
switch contacts.count {
case 1: completion([.success(with: contacts.first ?? INPerson(personHandle: INPersonHandle(value: "1800-olakase", type: .phoneNumber), nameComponents: nil, displayName: "ola k ase", image: nil, contactIdentifier: nil, customIdentifier: INPersonHandle(value: "1800-olakase", type: .phoneNumber).value))])
case 2...Int.max: completion([.disambiguation(with: contacts)])
default: completion([.unsupported()])
}
})
}
func confirm(intent: INStartCallIntent, completion: @escaping (INStartCallIntentResponse) -> Void) {
let userActivity = NSUserActivity(activityType: NSStringFromClass(INStartCallIntent.self))
let response = INStartCallIntentResponse(code: .ready, userActivity: userActivity)
completion(response)
}
}
在 Info.plist
中将 INStartCallIntent
添加到 IntentsSupported
因此,当 func handle(intent: INStartCallIntent, completion: @escaping (INStartCallIntentResponse) -> Void)
中的代码完成时应该将 NSUserActivity
发送到我的应用程序委托直接发送到 func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool
... 但不会触发该功能。目前我的应用无法拨打新电话,因为没有获得任何 userActivity
来检索联系人数据。
我也试过 func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool
但还是不行。
为了更具体,我遵循这个 tutorial 但改变了
INStartAudioCallIntent
for INStartCallIntent
because 已弃用。我不知道 swiftUI 是否是问题所在,我使用 @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
将我的应用程序委托添加到 swiftUI life cicle
是的,我的应用已请求 Siri 授权并已启用。有什么建议吗?我错过了什么?
好的,我想通了:问题是我试图从我的 AppDelegate 的方法 func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool
中获取 NSUserActivity
被触发,忘记它,如果你正在使用 SwiftUI,请不要遵循 documentation from apple,因为它不再工作了。
对于 SwiftUI,你必须实现 .onContinueUserActivity()
修饰符来获取 NSUserActivity
,你可以从这里做任何你需要做的事情。示例代码:
WindowGroup {
ContentView().onContinueUserActivity(NSStringFromClass(INStartCallIntent.self), perform: { userActivity in
//do something
})
}
实际上是在 swiftUI 中制作应用程序并尝试处理 Siri 意图以在我的应用程序中开始通话。我在我的项目中创建了一个扩展目标,在主目标和扩展目标中添加了 Siri 功能和应用程序组,我的意图处理程序完全正确地触发了 Siri 请求,启动 Siri 并要求拨打某些联系人的电话完美无缺,我的应用程序启动了此时应该从意图中收到 activity,但什么也没发生...
我的意图处理程序如下所示:
import Intents
class IntentHandler: INExtension, INStartCallIntentHandling {
func handle(intent: INStartCallIntent, completion: @escaping (INStartCallIntentResponse) -> Void) {
let userActivity = NSUserActivity(activityType: NSStringFromClass(INStartCallIntent.self))
guard intent.contacts?.first?.personHandle?.value != nil else {
completion(INStartCallIntentResponse(code: .failureContactNotSupportedByApp, userActivity: userActivity))
return
}
let response = INStartCallIntentResponse(code: .continueInApp, userActivity: userActivity)
completion(response)
}
func resolveContacts(for intent: INStartCallIntent, with completion: @escaping ([INStartCallContactResolutionResult]) -> Void) {
var contactName = ""
if let contacts = intent.contacts {
contactName = contacts.first?.displayName ?? ""
}
//This shared method is used to get contact data for completion
DataManager.sharedManager.findContact(contactName: contactName, with: {
contacts in
switch contacts.count {
case 1: completion([.success(with: contacts.first ?? INPerson(personHandle: INPersonHandle(value: "1800-olakase", type: .phoneNumber), nameComponents: nil, displayName: "ola k ase", image: nil, contactIdentifier: nil, customIdentifier: INPersonHandle(value: "1800-olakase", type: .phoneNumber).value))])
case 2...Int.max: completion([.disambiguation(with: contacts)])
default: completion([.unsupported()])
}
})
}
func confirm(intent: INStartCallIntent, completion: @escaping (INStartCallIntentResponse) -> Void) {
let userActivity = NSUserActivity(activityType: NSStringFromClass(INStartCallIntent.self))
let response = INStartCallIntentResponse(code: .ready, userActivity: userActivity)
completion(response)
}
}
在 Info.plist
INStartCallIntent
添加到 IntentsSupported
因此,当 func handle(intent: INStartCallIntent, completion: @escaping (INStartCallIntentResponse) -> Void)
中的代码完成时应该将 NSUserActivity
发送到我的应用程序委托直接发送到 func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool
... 但不会触发该功能。目前我的应用无法拨打新电话,因为没有获得任何 userActivity
来检索联系人数据。
我也试过 func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool
但还是不行。
为了更具体,我遵循这个 tutorial 但改变了
INStartAudioCallIntent
for INStartCallIntent
because 已弃用。我不知道 swiftUI 是否是问题所在,我使用 @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
将我的应用程序委托添加到 swiftUI life cicle
是的,我的应用已请求 Siri 授权并已启用。有什么建议吗?我错过了什么?
好的,我想通了:问题是我试图从我的 AppDelegate 的方法 func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool
中获取 NSUserActivity
被触发,忘记它,如果你正在使用 SwiftUI,请不要遵循 documentation from apple,因为它不再工作了。
对于 SwiftUI,你必须实现 .onContinueUserActivity()
修饰符来获取 NSUserActivity
,你可以从这里做任何你需要做的事情。示例代码:
WindowGroup {
ContentView().onContinueUserActivity(NSStringFromClass(INStartCallIntent.self), perform: { userActivity in
//do something
})
}