当前位置: 首页 > news >正文

Siri shortcuts 指北

系统支持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视频:

developer.apple.com/videos/play…

苹果Demo地址:

developer.apple.com/documentati…

文档: developer.apple.com/documentati…

转载于:https://juejin.im/post/5c18f80ee51d4534cc5a7ecf

相关文章:

  • day15生成器面试题和内置函数
  • idea debug
  • 关于XML文件
  • SQL自动流水号函数
  • NOI2018屠龙勇士(扩展CRT + splay(multiset))
  • 4 Redis 配置文件介绍
  • 定时任务Cron常用表达式与在线生成器
  • str()函数
  • Codeforces 1087C Connect Three (思维+模拟)
  • 网络图片转换到本地并转换成base64位
  • 最新最全 中文版Pycharm 2017安装教程 Python编译器安装(小白教程)
  • Spring事务管理要点总结
  • flask实现基于elasticsearch的关键词搜索建议
  • MySQL binlog group commit--commit stage
  • 返回多个值的摘要函数
  • JS 中的深拷贝与浅拷贝
  • [译] React v16.8: 含有Hooks的版本
  • 【技术性】Search知识
  • CSS盒模型深入
  • 从地狱到天堂,Node 回调向 async/await 转变
  • 基于遗传算法的优化问题求解
  • 简单实现一个textarea自适应高度
  • 开源SQL-on-Hadoop系统一览
  • 如何使用 JavaScript 解析 URL
  • 通过来模仿稀土掘金个人页面的布局来学习使用CoordinatorLayout
  • 以太坊客户端Geth命令参数详解
  • 追踪解析 FutureTask 源码
  • 400多位云计算专家和开发者,加入了同一个组织 ...
  • Linux权限管理(week1_day5)--技术流ken
  • ​DB-Engines 12月数据库排名: PostgreSQL有望获得「2020年度数据库」荣誉?
  • #ifdef 的技巧用法
  • $forceUpdate()函数
  • (附源码)springboot助农电商系统 毕业设计 081919
  • (十五)devops持续集成开发——jenkins流水线构建策略配置及触发器的使用
  • (十一)图像的罗伯特梯度锐化
  • (转)创业的注意事项
  • ***监测系统的构建(chkrootkit )
  • ***原理与防范
  • .locked1、locked勒索病毒解密方法|勒索病毒解决|勒索病毒恢复|数据库修复
  • .NET Framework 服务实现监控可观测性最佳实践
  • .NET 编写一个可以异步等待循环中任何一个部分的 Awaiter
  • .net通用权限框架B/S (三)--MODEL层(2)
  • :中兴通讯为何成功
  • @ConfigurationProperties注解对数据的自动封装
  • @Query中countQuery的介绍
  • @RequestBody与@ModelAttribute
  • @Transient注解
  • @在php中起什么作用?
  • [ linux ] linux 命令英文全称及解释
  • [\u4e00-\u9fa5] //匹配中文字符
  • [2]十道算法题【Java实现】
  • [C#]猫叫人醒老鼠跑 C#的委托及事件
  • [CLR via C#]11. 事件
  • [EFI]Dell Inspiron 15 5567 电脑 Hackintosh 黑苹果efi引导文件
  • [Linux](15)线程基础,线程控制,线程的互斥与同步