Siri自定义Intent以及处理

一、新建自定义Intent

1.Xcode->New->File->搜索Intent

2.左下角'+'号新建Intent
3.完善Intent资料
注意:1.Intent的命名,如果命名为A,编译之后,系统自动生成AIntent.h文件,import AIntent.h即可使用。

2.Paramters:你想要Intent传递的数据,自定义即可。

3.ShortCut Types:根据传入Intent的不同参数组合,生成不同的标题和副标题,主要用于在SpotLight中的显示或锁屏时的显示。

4.Response:你需要response中传递的数据。

4.编译

编译之后,会自动生成Intent Class和Intent Response以及IntentHandling协议。例如Intent命名为A,编译之后会自动生成AIntentAIntentResponseAIntentHandling协议(下文会用到),之后在App Extension中新建的处理AIntent的IntentHandler必须遵循AIntentHandling

二、使用自定义Intent

下述代码的作用是生成ShortCut,该ShortCut的类型是你自定义的Intent,执行下述代码之后,生成的ShortCut会在SpotLight或锁屏时展示,前提是在iPhone 设置->Siri与搜索中打开搜索建议,查询建议,锁屏建议,之后可以将ShortCut与用户语音对应。

f (@available(iOS 12.0, *)) {
        TestIntent *testIntent = [[TestIntent alloc] init];
        testIntent.content = @"新生成的App内处理的ShortCutIntent";
        INInteraction *interaction = [[INInteraction alloc] initWithIntent:testIntent response:nil];
        [interaction donateInteractionWithCompletion:^(NSError * _Nullable error) {
            
        }];
    } else {
        // Fallback on earlier versions
    }
复制代码

注意代码中的content参数,和Intent中的Parameters对应

三、处理自定义Intent

用户喊出语音之后,需要对其作出处理,分为App被处理和App Extension处理,区别是是否需要启动App

1.App内处理
if([userActivity.interaction.intent isKindOfClass:[TestIntent class]]){
        ViewControllerA *viewControllerA = [[ViewControllerA alloc] init];
        //根据userActivity.interaction.intent.content不同做不同的处理。
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:viewControllerA];
        [self.window.rootViewController presentViewController:nav animated:YES completion:nil];
    }
复制代码
2.App Extension处理

(1)Xcode->New->Target->搜索Intents Extension

(2)新建一个AIntentHandler来处理你自定义的AIntent,需要遵循上述的AIntentHandling协议。

TestIntentHandler.h

#import <Intents/Intents.h>
#import "TestIntent.h"

NS_ASSUME_NONNULL_BEGIN

@interface TestIntentHandler : INExtension<TestIntentHandling>

@end

NS_ASSUME_NONNULL_END
复制代码

TestIntentHandler.m

#import "TestIntentHandler.h"

@implementation TestIntentHandler

- (void)handleTest:(TestIntent *)intent completion:(void (^)(TestIntentResponse *response))completion NS_SWIFT_NAME(handle(intent:completion:)){
    NSUserActivity *userActivity = [[NSUserActivity alloc] initWithActivityType:NSStringFromClass([TestIntent class])];
    TestIntentResponse *response = [[TestIntentResponse alloc] initWithCode:TestIntentResponseCodeSuccess userActivity:userActivity];
    completion(response);
}

@end
复制代码

(3)在IntentHandler文件中的handlerForIntent方法中添加自定义Intent的指定IntentHandler。

- (id)handlerForIntent:(INIntent *)intent {
    // 这是默认的实现,如果你想要不同的对象来处理不同的意图,你可以覆盖重写
  if([intent isKindOfClass:[TestIntent class]]){
        TestIntentHandler *testIntentHandler = [[TestIntentHandler alloc] init];
        return testIntentHandler;
    }else{
        return self;
    }
}
复制代码

处理逻辑应该在AIntentHandler中编写。

Demo地址

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值