6

我在 Xcode 10(iOS 12 Beta)中捐赠自定义意图时遇到问题。

我创建了一个在我的主应用程序目标和我的“OrderIntent”目标之间共享的自定义框架。

我创建了一个 .intentdefinition 文件,其中目标成员资格设置为我的自定义框架(下面的屏幕截图)。

在此处输入图像描述

我在主应用程序中嵌入了一个“意图扩展”和“意图 UI 扩展”。

我还在添加意图扩展时创建的两个 info.plist 文件中将“OrderIntent”添加到 NSExtension->NSExtensionAttributes->IntentsSupported(下面的屏幕截图)。

在此处输入图像描述

我正在尝试像这样捐赠意图:

INPreferences.requestSiriAuthorization { (status) in

        if status != INSiriAuthorizationStatus.authorized {return}

        let orderIntent = OrderIntent()
        orderIntent.product = "Test product"
        orderIntent.quantity = 2

        let interaction = INInteraction(intent: customIntent, response: nil)

        interaction.donate { (error) in
            if error != nil {2
                if let error = error as NSError? {
                    print(error)
                }
            } else {
                print("success")
            }
        }
    }

上面的代码在用户点击应用程序中的按钮时运行。

我还设置了这样的意图处理程序:

class IntentHandler: INExtension {

override func handler(for intent: INIntent) -> Any {

    switch intent {
        case is OrderIntent:
            return OrderIntentHandler()

        default:
            fatalError()
    }

    return self
}

OrderIntentHandler 是这样的:

public class OrderIntentHandler: NSObject, OrderIntentHandling {

    public func handle(intent: OrderIntent, completion: @escaping (OrderIntentResponse) -> Void) {

        let orderIntentResponse = OrderIntentResponse(code: OrderIntentResponseCode.success, userActivity: nil)

        completion(orderIntentResponse)
    }

    public func confirm(intent: OrderIntent, completion: @escaping (OrderIntentResponse) -> Void) {

        let response = OrderIntentResponse(code: OrderIntentResponseCode.ready, userActivity: nil)

        completion(response)
    }
}

在设备上对此进行测试时,出现以下错误:

错误域 = IntentsErrorDomain 代码 = 1901 “此应用程序不支持捐赠意图 'OrderIntent'。请确保您有支持此意图的扩展。” UserInfo={NSLocalizedDescription=此应用程序不支持捐赠意图“OrderIntent”。请确保您有支持此意图的扩展程序。}

我不明白为什么应用程序不支持“OrderIntent”。

我已经在功能选项卡中启用了 Siri,并请求了用户的许可等。

是否需要采取任何其他步骤才能让我捐赠意图?

4

3 回答 3

2

我相信你的问题的答案在你的描述中是正确的(强调我的):

我创建了一个 .intentdefinition 文件,其中目标成员资格设置为我的自定义框架

我假设执行调用的代码interaction.donate在您的应用程序中,而不是在共享框架中。如果是这种情况(根据您对解决问题的方式的回答来判断),您还需要将主应用程序添加为.intentdefinition文件中的目标。您将不希望生成任何类,因为它们已经在您的应用链接到的共享框架中。您可能会遇到问题,因为看起来您在单独的 Xcode 项目中创建了共享框架,因此其他目标不可见。

于 2018-07-07T18:54:16.473 回答
0

尝试从-> ->的“Order Intent ”字符串中删除“Intent”一词。NSExtensionNSExtensionAttributesIntentsSupported

即您的路径将类似于NSExtension-> NSExtensionAttributes-> IntentsSupported-> Item 0-> Order

我知道Apple 文档指出我们应该:

将每个项目的值设置为意图的类名

但对我来说,它根本不起作用。

希望对你有帮助:)

于 2018-06-12T13:40:18.030 回答
0

通过将 .intentdefinition 文件拖入我的应用程序文件夹以及我的自定义框架文件夹(见下图),我已经能够通过在应用程序文件夹中添加对 CustomIntents.intentdefinition 文件的引用来使其工作:

在此处输入图像描述

复制文件时,我确保不要选中“如果需要则复制项目”选项,因此 .intentdefinition 文件仅存在于一个位置:

在此处输入图像描述

在设备上运行和测试后,交互已成功捐赠,应用程序现在在设备设置中的“Siri 和搜索”下提供“订购测试产品”选项。

于 2018-06-12T17:19:22.280 回答