快捷指令

iOS Siri Kit 添加一个自己的快捷指令

2021-09-17  本文已影响0人  不负时光_

第一步 创建一个 Intents 选择siriKit

5BC4E856-D403-457F-8CA4-C47D929EB798.png
接下来 配置这个 Intents
91A65B42-ECED-4A0A-AC5D-389EBC83AC3C.png
FA3B0D48-D9B7-4041-8C61-010B3F099191.png
A08D2E3B-BF73-46B8-A2A6-22AD34C47357.png
99F89395-DBDB-4AB4-ACBF-40C78D0E5055.png
到这 前期的准备工作完成

第二步

开始 使用代码将指令添加到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];
}
添加完这些后我们就可以调起 系统的添加到指令界面了
IMG_36314F274BE1-1.jpeg

第三步

添加指令成功后,我们就要去响应我们设置的指令内容了
创建一个IntentHandler 去处理
在项目的总设置地方添加 类似添加widget
1E94FABC-B14D-43CE-A8F8-3F2269DED69C.png
23809554-0502-45E6-8D2F-3E55E9D39A84.png
创建成功后,在项目文件内就可以看到两个新的文件
8AE4DBCA-1EBC-4949-9D3B-2D926710593E.png
因为要处理自己的业务逻辑 所以我自定义了一个Handler
BF2E59C0-C7CB-410D-B379-D1D8236C5E17.png
在.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) {
}

}
上一篇下一篇

猜你喜欢

热点阅读