SiriKit 学习笔记(二) 创建Intents Extension

Intents Extention是一个app程序包中的app扩展,首先你必须有一个app去支持它。 Intents Extention处理大部分与SiriKit的交互。


给App程序加入Intents Enxtention, 请做以下操作


  • 在IOS app中启用Siri
  • XcodeProject中加入Intents Extention target, 并配置到Info.plist, 
  • 授权运行中使用Siri
  • 定义handle intents的objects
  • 定义 app特有的词库


配置Xcode Project


启用Siri  Capability:
  • 打开Xcode Project
  • 在project settings, 选择iOS app target
  • 选择 Capabilities tab
  • 启用 Siri Capability


添加Intents extension
  • 在Xcode中打开iOS app project
  • 选择File > New > Target
  • 在相应平台的Application Extension group中选择Intents extension
  • 点击Next
  • 选择名字和其他相应配置 
  • Finish

注意:你可以添加多个Intents Extension,每一个Extension仅支持不同的Intents


添加Intents extension之后,配置Info.plist (支持的indents范围), SiriKit通过该配置信息决定如何引导intetns到extension


划清extension支持的intents范围:
打开Intents extension的Info.plist文件
展开NSExtension和NSExtensionAttributes 得到 IntentsSupported和IntentsRestrictedWhileLocked
在IntentsSupported(必须)中给每个intent添加String, 值为该值intent的class name
在IntentsRestrictedWhileLocked(可选),为所有需要unlock条件的intents添加string 值,同上为class name
以下引用英文文档原文(中文表达不太精确)或参考官方原文档:
  • 1. In Xcode, select the Info.plist file of your Intents extension.
  • 2. Expand the NSExtension and NSExtensionAttributes keys to reveal the IntentsSupported andIntentsRestrictedWhileLocked keys.
  • 3. In the IntentsSupported key, add a String item for each intent that the extension handles. Set the value of each item to the class name of the intent.
  • This key is required. You can support all of the intents in a given domain or only some of them, and a single extension can support multiple domains.
  • 4. In the IntentsRestrictedWhileLocked key, add a String item for each intent for which you require the device to be unlocked. Set the value of each item to the class name of the intent.
This key is optional. Some intents, such as those involving financial transactions, always require the user’s device to be unlocked. You can use this key to augment the default list with intents that do not require an unlocked device by default.


当用户指令模糊不清导致系统得出多个intents时,SiriKit使用IntentsSupporte的字键排序并发送到app,所以需要特变关注这个顺序问题。
比如用户口令:发送信息有两种intents > 打电话和发送文本信息,当打电话为优时会被选择。




你的iOS APP 获取Siri授权


如何授权APP使用SiriKit:


在Info.plist中包括NSSiriUsageDescription,这个键对应的值是app使用方式的描述String,比如”Workout information will be sent to Siri”
调用requestSiriAuthorization:  class INPreferences的方法


第一次调用requestSiriAuthorization的时候系统会弹出警告,警告内容含Info.plist里面定义好的NSSiriUsageDescription值


重定App服务

Intents extension 相当于app的代理,所以有一些服务会和app原有的重复,至此,你必须考虑重构你的代码因为可能会有很多任务是重复的:

将核心服务打包成独立框架已让app或intents extension扩展共同使用

讲公用的资源放到一个共享容器中


使用 Intents Framwork:

  • 利用INVocabulary class注册用户自定义的词库
  • 利用INPreferences class作权限管理等
  • 创建INInteraction object向系统提供更多互动资料                      


你可能感兴趣的:(swift_Siri)