我知道有一个内置的模板。
我转到文件菜单,然后选择新建>目标
从左侧窗格中选择iOS>应用程序扩展。
现在选择Intents扩展。
这将创建两个新组:YourExtension和YourExtensionUI。如果打开YourExtension组,您将看到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)
}
}
我做到了,可以。
现在我的问题是关于使用INStartWorkoutIntent而不是INSendMessageIntent,我应该怎么做?也有用于此目的的内置模板吗?
最后,我自己解决了这个问题。
当您想正确使用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)
}
不要忘记:
对于新创建的Intent目标,请完全展开NSExtension词典以研究其内容。该词典更详细地描述了扩展支持的意图,以及是否要允许用户在设备锁定时调用意图。
如果您想支持多个主题,请在顶部插入最相关的意图。 Siri使用此顺序来找出用户要使用的模棱两可的情况。
我们现在需要定义我们要支持的意图。
例如,我要构建一个支持付款方式的扩展程序。修改Info.plist文件以匹配下图。
这里,我们指定要处理INSendPaymentIntent,并要求设备解锁。我们不希望陌生人在设备丢失或被盗时发送付款!
仅在运行时将目标设置为目标就完成了。