Siri shortcuts 指北

栏目: IOS · 发布时间: 6年前

内容简介:选择File → New → Target,勾选Include UI Extension,可以使用自定义UI扩展。方法:File → New → File创建完后如下界面:
Siri shortcuts 指北

添加新的target

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

Siri shortcuts 指北

创建Intent Definition File

方法:File → New → File

Siri shortcuts 指北

新建Intent

Siri shortcuts 指北

创建完后如下界面:

Siri shortcuts 指北

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

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

点击如下图位置:

Siri shortcuts 指北

开发过程

权限获取

获取当前权限

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

请求获取权限

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

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

Siri shortcuts 指北

添加Siri快捷指令页面

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

Siri shortcuts 指北

代码如下:

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;
}
复制代码

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

程序员成长的烦恼

程序员成长的烦恼

吴亮、周金桥、李春雷、周礼 / 华中科技大学出版社 / 2011-4 / 28.00元

还在犹豫该不该转行学编程?还在编程的道路上摸爬滚打?在追寻梦想的道路上你并不孤单,《程序员成长的烦恼》中的四位“草根”程序员也曾有过类似的困惑。看看油田焊接技术员出身的周金桥是如何成功转行当上程序员的,做过钳工、当过外贸跟单员的李春雷是如何自学编程的,打小在486计算机上学习编程的吴亮是如何一路坚持下来的,工作中屡屡受挫、频繁跳槽的周礼是如何找到出路的。 《程序员成长的烦恼》记录了他们一步一......一起来看看 《程序员成长的烦恼》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具