如何在您的ios应用程序中添加Intents扩展程序?

时间:2016-06-16 07:02:29

标签: ios swift ios10

如何在您的ios应用中添加Intents扩展程序

1 个答案:

答案 0 :(得分:2)

它有一个内置的模板,虽然我承认它需要一些工作。

  1. 创建所选模板的常规iOS应用。
  2. 转到“文件”菜单,然后选择“新建”>目标
  3. 选择iOS>左侧窗格中的应用程序扩展。
  4. 现在选择Intents扩展名。
  5. 这将创建两个新组:YourExtension和YourExtensionUI。如果您打开YoureExtension组,您将看到IntentHandler.swift,其中包含一些用于处理锻炼的示例代码。如果您刚开始使用,我建议完全放弃该代码,因为它的时间比它需要的时间长。

    这是一个让你开始的更简单的例子:

    class IntentHandler: INExtension, INSendMessageIntentHandling {
        override func handler(for intent: INIntent) -> AnyObject {
            // 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.
            return self
        }
    
        func handle(sendMessage intent: INSendMessageIntent, completion: (INSendMessageIntentResponse) -> Void) {
            print("Send message: " + (intent.content ?? "No message"))
    
            let response = INSendMessageIntentResponse(code: .success, userActivity: nil)
            completion(response)
        }
    }
    

    I wrote about this, and other iOS 10 features, in more detail here.