首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >iOS -启动Siri intent时AppDelegate未检索userActivity

iOS -启动Siri intent时AppDelegate未检索userActivity
EN

Stack Overflow用户
提问于 2021-02-24 07:25:30
回答 1查看 317关注 0票数 0

实际上是用swiftUI制作了一个应用程序,并试图处理Siri在我的应用程序中发起呼叫的意图。我在我的项目中做了一个扩展目标,在主目标和扩展目标中添加了Siri功能和应用程序组,我的意图处理程序完全正确地触发了Siri请求,启动Siri并请求呼叫一些联系人工作得很好,我的应用程序启动了,在这一点上应该会收到来自意图的活动,但什么都没有发生……

我的意图处理程序如下所示:

代码语言:javascript
复制
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)
    }
}

添加INStartCallIntentIntentsSupportedInfo.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授权。有什么建议吗?我漏掉了什么吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 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,从这里你可以做任何你需要做的事情。示例代码:

代码语言:javascript
复制
WindowGroup {
    ContentView().onContinueUserActivity(NSStringFromClass(INStartCallIntent.self), perform: { userActivity in
        //do something
    })
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66342540

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档