我知道有一个内置模板。
我转到"file"菜单并选择“新建”>“目标”
从左侧 Pane 中选择“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 时,只需删除所有内置模板内容即可。
您还必须将 INSendMessageIntentHandling 替换为 INStartWorkoutIntent Handling。
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)
}
不要忘记:
对于新创建的 Intents 目标,完全展开 NSExtension 字典来研究其内容。该词典更详细地描述了您的扩展程序支持哪些意图,以及您是否希望允许用户在设备锁定时调用意图。
如果您想支持多个意图,请在顶部插入最相关的意图。 Siri 使用这一顺序来确定用户想要使用哪一个,以防出现歧义。
我们现在需要定义我们想要支持哪些意图。
例如,我想构建一个支持付款意图的扩展。修改Info.plist文件以匹配下图。
这里我们指定要处理 INSendPaymentIntent 并且需要解锁设备。我们不希望陌生人在设备丢失或被盗时发送付款!
最后一件事只是在运行时设置目标 Intent,然后就完成了。
关于ios - 如何在 Swift 3 中正确初始化 INStartWorkoutIntent?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43159662/