I love iOSSiri开发Siri开发

Siri自定义Intent及ShortCut

2018-09-11  本文已影响13人  乳猪啸谷

一、新建自定义Intent

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

image
2.左下角'+'号新建Intent
image
3.完善Intent资料
image
注意: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地址

上一篇下一篇

猜你喜欢

热点阅读