实际上是用swiftUI制作了一个应用程序,并试图处理Siri在我的应用程序中发起呼叫的意图。我在我的项目中做了一个扩展目标,在主目标和扩展目标中添加了Siri功能和应用程序组,我的意图处理程序完全正确地触发了Siri请求,启动Siri并请求呼叫一些联系人工作得很好,我的应用程序启动了,在这一点上应该会收到来自意图的活动,但什么都没有发生……
我的意图处理程序如下所示:
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)
}
}
添加INStartCallIntent
至IntentsSupported
在Info.plist
所以,当代码在func handle(intent: INStartCallIntent, completion: @escaping (INStartCallIntentResponse) -> Void)
did complete应该发送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
对于INStartCallIntent
因为已被弃用。我不知道swiftUI是否是问题所在,我用了@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
要将我的应用程序委派添加到swiftUI生命周期,请执行以下操作
是的,我的应用程序已经启用了request Siri授权。有什么建议吗?我漏掉了什么吗?
发布于 2021-02-25 05:51:29
好的,我想通了:问题是我试图让NSUserActivity
从方法func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool
来自我的AppDelegate这永远不会被触发,忘记这一点,不要遵循documentation from apple如果您使用的是SwiftUI,因为它不再起作用。
对于SwiftUI,您必须实现.onContinueUserActivity()
修饰符来获取NSUserActivity
,从这里你可以做任何你需要做的事情。示例代码:
WindowGroup {
ContentView().onContinueUserActivity(NSStringFromClass(INStartCallIntent.self), perform: { userActivity in
//do something
})
}
https://stackoverflow.com/questions/66342540
复制相似问题