2017-04-01 41 views
4

我知道它有一个内置的模板。Swift 3 - 如何正确启动INStart锻炼意图?

我去File菜单并选择New>从左侧窗格中目标

选择的iOS>应用程序扩展。

现在选择Intents扩展。

这将创建两个新组: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) 
    } 
} 

我这样做,它的确定。

现在我的问题是关于使用INStart​Workout​Intent而不是INSendMessageIntent,我该怎么办?这种意图是否有内置模板?

回答

6

最后,我自己解决了这个问题。

当您想正确使用INStartWorkoutIntent时,您只需删除所有内置的模板内容。

您还需要通过INStartWorkoutIntent处理来替换INSendMessageIntentHandling。

public func handle(startWorkout intent: INStartWorkoutIntent, completion: @escaping (INStartWorkoutIntentResponse) -> Swift.Void) { 
    let userActivity = NSUserActivity(activityType: NSStringFromClass(INStartWorkoutIntent.self)) 
    let response = INStartWorkoutIntentResponse(code: .continueInApp, userActivity: userActivity) 
    completion(response) 
} 

不要忘记:

到新创建的意图目标,完全展开NSExtension词典,学习的内容。该字典更详细地描述了您的扩展支持哪些意图,以及您是否希望允许用户在设备锁定时调用意图。

如果您想要支持多个插入,请在顶部插入最相关的意图。 Siri使用这个命令来确定用户在模糊不清的情况下想要使用哪一个。

我们现在需要定义我们想要支持哪些意图。

例如,我想构建支持付款意向的扩展。修改Info.plist文件以匹配下面的图片。

enter image description here

在这里,我们指定我们要处理的INSendPaymentIntent,而我们需要的设备进行解锁。当设备丢失或被盗时,我们不希望陌生人发送付款!

最后一件事情就是在目标上设置你的意图,并且完成了。