博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Siri shortcuts 指北
阅读量:7080 次
发布时间:2019-06-28

本文共 3214 字,大约阅读时间需要 10 分钟。

系统支持iOS 12

工程搭建

开启工程权限

添加新的target

选择File → New → Target,勾选Include UI Extension,可以使用自定义UI扩展。

创建Intent Definition File

方法:File → New → File

新建Intent

创建完后如下界面:

可以看到Intent是一个Category,我们可以设置类型(标示Intent的作用),添加参数(根据Siri解析命令传入),添加标题,描述(这些会显示在Siri唤醒我们app的时候)。

编译的时候系统会自动生成一个子类XXXIntent : INIntent,我们需要找到这个类,使用这个类来进行我们的其他操作。

点击如下图位置:

开发过程

权限获取

获取当前权限

INSiriAuthorizationStatus siriStatus = [INPreferences siriAuthorizationStatus];复制代码

请求获取权限

[INPreferences requestSiriAuthorization:^(INSiriAuthorizationStatus status) {                switch (status) {                    case INSiriAuthorizationStatusAuthorized: // 成功获取权限                        NSLog(@"权限获取成功");                        break;                    case INSiriAuthorizationStatusDenied: // 成功获取权限                        NSLog(@"权限获取用户拒绝");                        break;                                            default:                        break;                }            }];复制代码

注意添加info.plist(或者多语言)设置提示语,否则权限请求不会弹出。

添加Siri快捷指令页面

调用系统API,调用如下页面。

代码如下:

GotoPageIntent *intent = [[GotoPageIntent alloc] init]; // GotoPageIntent为我们自定义的Intent,找不到看上面    intent.suggestedInvocationPhrase = @"打开app"; // 这是建议的提示语,会展示在页面上    INShortcut *shortcurt = [[INShortcut alloc] initWithIntent:intent];        INUIAddVoiceShortcutViewController *addvc = [[INUIAddVoiceShortcutViewController alloc] initWithShortcut:shortcurt];    addvc.delegate = self;    [self presentViewController:addvc animated:YES completion:nil];复制代码

处理回调:

/*! @abstract Called after the user finishes the setup flow for the voice shortcut, with either the successfully-added voice shortcut, or an error. @discussion Your implementation of this method should dismiss the view controller. */- (void)addVoiceShortcutViewController:(INUIAddVoiceShortcutViewController *)controller didFinishWithVoiceShortcut:(nullable INVoiceShortcut *)voiceShortcut error:(nullable NSError *)error; {    if (!error) {        [controller dismissViewControllerAnimated:YES completion:nil];    }}/*! @abstract Called if the user cancels the setup flow; the voice shortcut was not added. @discussion Your implementation of this method should dismiss the view controller. */- (void)addVoiceShortcutViewControllerDidCancel:(INUIAddVoiceShortcutViewController *)controller; {    [controller dismissViewControllerAnimated:YES completion:nil];}复制代码

处理Siri Shortcuts触发的回调

  • 自定义xxxIntentHandler,继承自NSObject。遵循xxxIntentHandling(这个是我们Xcode自己生成的,找的方法见上面创建Intent Definition File中),在里面实现我们需要的逻辑。
- (void)handleGotoPage:(GotoPageIntent *)intent completion:(void (^)(GotoPageIntentResponse *response))completion NS_SWIFT_NAME(handle(intent:completion:)) {    // GotoPageIntentResponseCodeContinueInApp 打开app    // GotoPageIntentResponseCodeSuccess 回调成功    GotoPageIntentResponse *response = [[GotoPageIntentResponse alloc] initWithCode:GotoPageIntentResponseCodeSuccess userActivity:nil];    completion(response);}复制代码
  • 打开我们的Extension,IntentHander方法。处理我们自己定义的Intent。
- (id)handlerForIntent:(INIntent *)intent {    if ([intent isKindOfClass:[GotoPageIntent class]]) {        return [[GotoAppIntentHandler alloc] init];    }        return self;}复制代码

自定义 ExtensionUI

可以自定义Siri呼出app的样式,在IntentViewController中开发。

参考资料

WWDC视频:

苹果Demo地址:

文档:

转载地址:http://dcvml.baihongyu.com/

你可能感兴趣的文章
unix编程之多进程编程
查看>>
R语言学习之聚类
查看>>
我的友情链接
查看>>
斯坦佛编程教程-Unix编程工具(三)
查看>>
DHCP和TFTP配置以及CentOS 7上的服务控制
查看>>
Python 5.5 使用枚举类
查看>>
cookie禁用后session id传值的问题
查看>>
android 动画AnimationSet 和 AnimatorSet
查看>>
Dharma勒索软件继续大肆传播,据称已有100多家希腊网站沦陷
查看>>
成为JavaGC专家(1)—深入浅出Java垃圾回收机制
查看>>
Linux学习笔记(十七) vim
查看>>
三十二、iptables filter表小案例、iptables nat表应用
查看>>
Linux第一周学习笔记(4)
查看>>
袋鼠云数据中台专栏2.0 | 数据中台之数据集成
查看>>
当P4遇见NAT64,UCloud如何快速从IPv4向IPv6演进?
查看>>
iOS少用的框架
查看>>
ups锂电池的优势
查看>>
关于程序员和***区别在哪里?
查看>>
tomcat启动报错Invalid character found in method name. HTTP method names must be tokens
查看>>
appium+python3.6
查看>>