第一步 创建一个 Intents 选择siriKit
接下来 配置这个 Intents
到这 前期的准备工作完成
第二步
开始 使用代码将指令添加到Siri内
在绑定的界面中添加 这两个系统类 和 刚才自定义名称的Intent
#import <IntentsUI/IntentsUI.h>
#import <Intents/Intents.h>
#import "GJCustomIntent.h"
@property (nonatomic, strong) GJCustomIntentAPI_AVAILABLE(ios(12.0)) *intent;
@property (nonatomic, strong) INUIAddVoiceShortcutButtonAPI_AVAILABLE(ios(12.0)) *shortcutButton;
//懒加载 这里的title 和 sceneID 就是上面创建的两个字段,用于告诉siri这个绑定的指令是什么,方便当Siri触发后,再将这个字段返回给我们,让我们做业务处理
- (GJCustomIntent *)intentAPI_AVAILABLE(ios(12.0)){
if (!_intent) {
_intent = [[GJCustomIntentalloc] init];
_intent.title = @"自定义Title";
_intent.sceneID = @"123456”;
//在Siri语音设置时显示的建议设置唤起文字
_intent.suggestedInvocationPhrase = @"开始执行";
}
return_intent;
}
再需要触发的地方添加 系统的指令Button
if (@available(iOS 12.0, *)) {
_shortcutButton = [[INUIAddVoiceShortcutButtonalloc] initWithStyle:INUIAddVoiceShortcutButtonStyleWhiteOutline];
_shortcutButton.shortcut = [[INShortcutalloc] initWithIntent:self.intent];
_shortcutButton.translatesAutoresizingMaskIntoConstraints = false;
_shortcutButton.delegate = self;
[self.colorViewaddSubview:_shortcutButton];
}
添加指令触发的代理方法
< INUIAddVoiceShortcutButtonDelegate,
INUIAddVoiceShortcutViewControllerDelegate,
INUIEditVoiceShortcutViewControllerDelegate>
#pragma mark - INUIAddVoiceShortcutButtonDelegate
- (void)presentAddVoiceShortcutViewController:(INUIAddVoiceShortcutViewController *)addVoiceShortcutViewController forAddVoiceShortcutButton:(INUIAddVoiceShortcutButton *)addVoiceShortcutButton API_AVAILABLE(ios(12.0)){
addVoiceShortcutViewController.delegate = self;
[selfpresentViewController:addVoiceShortcutViewController animated:YEScompletion:nil];
}
- (void)presentEditVoiceShortcutViewController:(INUIEditVoiceShortcutViewController *)editVoiceShortcutViewController forAddVoiceShortcutButton:(INUIAddVoiceShortcutButton *)addVoiceShortcutButton API_AVAILABLE(ios(12.0)){
editVoiceShortcutViewController.delegate = self;
[selfpresentViewController:editVoiceShortcutViewController animated:YEScompletion:nil];
}
#pragma mark - INUIAddVoiceShortcutViewControllerDelegate
- (void)addVoiceShortcutViewController:(INUIAddVoiceShortcutViewController *)controller didFinishWithVoiceShortcut:(INVoiceShortcut *)voiceShortcut error:(NSError *)error
API_AVAILABLE(ios(12.0)){
[controller dismissViewControllerAnimated:YEScompletion:nil];
}
- (void)addVoiceShortcutViewControllerDidCancel:(INUIAddVoiceShortcutViewController *)controller API_AVAILABLE(ios(12.0)){
[controller dismissViewControllerAnimated:YEScompletion:nil];
}
#pragma mark - INUIEditVoiceShortcutViewControllerDelegate
- (void)editVoiceShortcutViewControllerDidCancel:(INUIEditVoiceShortcutViewController *)controller API_AVAILABLE(ios(12.0)){
[controller dismissViewControllerAnimated:YEScompletion:nil];
}
- (void)editVoiceShortcutViewController:(INUIEditVoiceShortcutViewController *)controller didUpdateVoiceShortcut:(INVoiceShortcut *)voiceShortcut error:(NSError *)error API_AVAILABLE(ios(12.0)){
[controller dismissViewControllerAnimated:YEScompletion:nil];
}
- (void)editVoiceShortcutViewController:(INUIEditVoiceShortcutViewController *)controller didDeleteVoiceShortcutWithIdentifier:(NSUUID *)deletedVoiceShortcutIdentifier API_AVAILABLE(ios(12.0)){
[controller dismissViewControllerAnimated:YEScompletion:nil];
}
添加完这些后我们就可以调起 系统的添加到指令界面了
第三步
添加指令成功后,我们就要去响应我们设置的指令内容了
创建一个IntentHandler 去处理
在项目的总设置地方添加 类似添加widget
创建成功后,在项目文件内就可以看到两个新的文件
因为要处理自己的业务逻辑 所以我自定义了一个Handler
在.m文件中 实现两个方法 用于自己的业务逻辑判断
每个自定义名称方法都会不同
// 准备阶段 如果选择了 用户询问的选项,那么这个就会作为第一层界面 让siri继续往下走,打开界面
//如果在这个方法就返回一个 IntentResponseCodeFailure 错误的返回,那么下面的界面都不会出现了
-(void)confirmGJCustom:(GJCustomIntent *)intent completion:(void (^)(GJCustomIntentResponse * _Nonnull))completion{
// intent.title
// intent.sceneID
//第一层 判读是否登录等权限
// NSUserDefaults *user =[NSUserDefaults standardUserDefaults];
// NSString *str = [user objectForKey:@"user_name"];
GJCustomIntentResponse *rsp = [[GJCustomIntentResponsealloc] initWithCode:GJCustomIntentResponseCodeReadyuserActivity:nil];
rsp.secenID = intent.sceneID;
rsp.title = intent.title;
completion(rsp);
}
// 触发阶段
- (void)handleGJCustom:(nonnullGJCustomIntent *)intent completion:(nonnullvoid (^)(GJCustomIntentResponse * ))completion {
GJCustomIntentResponse *rsp = [[GJCustomIntentResponsealloc] initWithCode:GJCustomIntentResponseCodeSuccessuserActivity:nil];
rsp.secenID = intent.sceneID;
rsp.title = intent.title;
completion(rsp);
}
回到接收指令的第一个入口 IntentHandler
- (id)handlerForIntent:(INIntent *)intent {
// This is the default implementation. If you want different objects to handle different intents,
// you can override this and return the handler you want for that particular intent.
if ([intent isKindOfClass:[GJCustomIntentclass]]) {
return [[GJCustomHandleralloc]init];
}
returnself;
}
这里的业务逻辑就处理完成了,下面就是Siri弹出框的界面展示
进入UI界面 IntentViewController.m
#pragma mark - INUIHostedViewControlling
// Prepare your view controller for the interaction to handle.
- (void)configureViewForParameters:(NSSet <INParameter *> *)parameters ofInteraction:(INInteraction *)interaction interactiveBehavior:(INUIInteractiveBehavior)interactiveBehavior context:(INUIHostedViewContext)context completion:(void (^)(BOOL success, NSSet <INParameter *> *configuredParameters, CGSize desiredSize))completion {
//根据不同的类型展示不同的界面
if (interaction.intentHandlingStatus == INIntentHandlingStatusReady) {
} elseif (interaction.intentHandlingStatus == INIntentHandlingStatusSuccess) {
}
}