如何让Siri创建笔记?

时间:2018-12-02 04:22:06

标签: ios siri sirikit

我正在尝试使用SiriKit创建笔记。这是我在Intents App Extension中的代码:

import Intents

class IntentHandler: INExtension {

    override func handler(for intent: INIntent) -> Any {
        // This is the default implementation.  If you want different objects to handle different intents,
        // you can override this and return the handler you want for that particular intent.

        print("IntentHandler.handler(_:)")

        guard let createNoteIntent = intent as? INCreateNoteIntent else {

            return self

        }

        if let textNoteContent = createNoteIntent.content as? INTextNoteContent {

            print(textNoteContent.text as Any)

        }

        return CreateEntry()

    }

}

import UIKit
import Intents

class CreateEntry: NSObject, INCreateNoteIntentHandling {

    func handle(intent: INCreateNoteIntent, completion: @escaping (INCreateNoteIntentResponse) -> Void) {

        print("CreateEntry.handle(_:_:)")

        if let textNoteContent = intent.content as? INTextNoteContent {

            print(textNoteContent.text as Any)

        }

        let note = INNote(title: INSpeakableString(spokenPhrase: "May the force be with you."), contents: [INNoteContent()], groupName: nil, createdDateComponents: nil, modifiedDateComponents: nil, identifier: nil)

        let response = INCreateNoteIntentResponse(code: INCreateNoteIntentResponseCode.ready, userActivity: nil)

        response.createdNote = note

        completion(response)

    }

}

当我问Siri“使用日记创建便笺”时,Siri回答“对不起,该应用程序有问题”。然后我的代码会显示这些结果。

  

IntentHandler.handler(_:)

     

IntentHandler.handler(_:)

     

IntentHandler.handler(_:)

     

IntentHandler.handler(_:)

     

IntentHandler.handler(_:)

     

CreateEntry.handle(:)

当我将INCreateNoteIntentResponse的INCreateNoteIntentResponseCode参数更改为成功时,Siri会做出回应,说该便笺已创建,然后向我显示该便笺的内容:“愿力量与您同在”。

Siri从未问过笔记中要说些什么。我想如果我像上面的代码中那样发送就绪响应,则Siri会问用户在笔记中写些什么。

我被困在这里。缺少相关文档。

1 个答案:

答案 0 :(得分:0)

Intent Handler中有一些可选方法需要实现。有关详细信息,请参见https://developer.apple.com/documentation/sirikit/increatenoteintenthandling

  • 应在resolve*方法中验证传递的参数。第一个参数INCreateNoteIntent具有标题,内容和组名字段。根据参数是应用程序的必需参数还是可选参数,可以使用适当的响应调用completion回调。这是部分验证。
  • 设置所有参数并需要完成最终验证时,将调用
  • confirm。您可以在此处发送.ready状态代码。
  • handle在需要执行意图时(在此意图中实际创建INNote)被调用。与其他呼叫一样,用户说的实际参数也出现在第一个参数中。