Siri Shortcuts intent 扩展开发

2020-12-18  本文已影响0人  小码农CC

直接开始
第一步: 添加文件Sirikit Intent Definition File

截屏2020-12-18 下午2.26.15.png

第二步:编辑文件

截屏2020-12-18 下午2.27.52.png

选择处理类别 generic 是直接运行 项目,其他的需要操作

截屏2020-12-18 下午2.28.42.png

如果选择了这个按钮也是需要点击后才开始执行,不选择则会直接运行

截屏2020-12-18 下午2.29.55.png

这里添加你想要跟业务相关的字段


截屏2020-12-18 下午2.32.34.png

在这里直接设置 可以在添加快捷指令页面显示


截屏2020-12-18 下午2.33.22.png

编译运行,会在右侧编辑栏看到


截屏2020-12-18 下午2.40.36.png

siriintent 就是你接下来需要用到的类
第二步:

添加intent ,创建intent 时候会提示是否同时创建intent UI,如果需要页面展示选择是

截屏2020-12-18 下午2.38.32.png

这块就是你创建好之后的文件目录

截屏2020-12-18 下午2.42.03.png

网上大多在讲解在intentHandler类中 导入你的intent文件头文件,你会发现此时会报头文件找不到

截屏2020-12-18 下午2.42.55.png

注意:此时你就需要在targets-》build phases 中添加第一步你所创建的intentdefinition文件,这样你在intent文件导入头文件时,就不会报错了,intent UI 中也需要这么做

截屏2020-12-18 下午2.44.56.png

第三步,代码处理

在intenthandler中根据intent业务类型不同判断处理逻辑

- (id)handlerForIntent:(INIntent *)intent {
    if ([intent isKindOfClass:[SiriIntent class]]) {
        SiriIntentHandler * siriHander = [[SiriIntentHandler alloc]init];
        return siriHander;
    }
    
     // 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.
    
    return self;
}

第四步,在你的intentViewController 类中 修改定制你的UI

- (void)configureViewForParameters:(NSSet <INParameter *> *)parameters ofInteraction:(INInteraction *)interaction interactiveBehavior:(INUIInteractiveBehavior)interactiveBehavior context:(INUIHostedViewContext)context completion:(void (^)(BOOL success, NSSet <INParameter *> *configuredParameters, CGSize desiredSize))completion {
    // Do configuration here, including preparing views and calculating a desired size for presentation.
    SiriIntentResponse *rsp = (SiriIntentResponse *) interaction.intentResponse;
       SiriIntent *intent = (SiriIntent *)interaction.intent;
    if (rsp.code == SiriIntentResponseCodeSuccess) {
        self.name.text = @"成功";
      if (completion) {
          completion(YES, parameters, [self desiredSize]);
      }
    }else{
        self.name.text = @"开始";
        if (completion) {
            completion(YES, parameters, [self desiredSize]);
        }
    }
}

在这个类中 添加你的业务逻辑
再次注意: 如果你的的业务逻辑也需要涉及到http请求,在 intent UI或者 intent 中的 info.plist文件中 添加
Allow Arbitrary Loads ---- yes

允许http请求

第五步,开始在你的主项目使用siri shourtcuts的地方添加调起代码

//
//  ViewController.m
//  siri-intents
//
//  Created by david on 2020/11/27.
//  Copyright © 2020 david. All rights reserved.
//

#import "ViewController.h"
#import "SiriIntent.h"
  #import <Intents/Intents.h>
#import <IntentsUI/IntentsUI.h>
 @interface ViewController ()< INUIEditVoiceShortcutViewControllerDelegate,INUIAddVoiceShortcutViewControllerDelegate>

@property(nonatomic,strong) INUIAddVoiceShortcutViewController *customShortCutViewController;

@property(nonatomic,strong) SiriIntent *testIntent;
@property(nonatomic,strong) SiriIntentResponse *testIntentResponse;

@property(nonatomic,strong) INInteraction *interaction;

@property(nonatomic,strong) INShortcut *shortcut;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"SiriTest";
       if (@available(iOS 12.0, *)) {
           
           [INPreferences requestSiriAuthorization:^(INSiriAuthorizationStatus status) {
               switch (status) {
                   case INSiriAuthorizationStatusNotDetermined:
                       NSLog(@"用户尚未对该应用程序作出选择。");
                       break;
                   case INSiriAuthorizationStatusRestricted:
                       NSLog(@"此应用程序无权使用Siri服务");
                       break;
                   case INSiriAuthorizationStatusDenied:
                       NSLog(@"用户已明确拒绝此应用程序的授权");
                       break;
                   case INSiriAuthorizationStatusAuthorized:
                       NSLog(@"用户可以使用此应用程序的授权");
                       break;
                   default:
                       break;
               }
               
           }];
       }
    UIButton *_addSiriBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, 151, 200, 50)];
    [_addSiriBtn setTitle:@"编辑siri" forState:UIControlStateNormal];
    [_addSiriBtn setTitleColor:UIColor.blueColor forState:UIControlStateNormal];
    [_addSiriBtn addTarget:self action:@selector(buildShortcutInCurrentViewController) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:_addSiriBtn];
   
 
    // Do any additional setup after loading the view.
}
 
-(void)buildShortcutInCurrentViewController
{
    self.testIntent = [[SiriIntent alloc] init];
//    self.testIntent.suggestedInvocationPhrase = @"";
    self.testIntent.name = @"张冲冲";
 
    
    self.interaction = [[INInteraction alloc] initWithIntent:self.testIntent response:nil];
    [self.interaction donateInteractionWithCompletion:^(NSError * _Nullable error) {
        if(error)
        {
            NSLog(@"%@",error);
        }
        else
        {
            NSLog(@"donate success");
        }
    }];
    
    if (@available(iOS 12.0, *)) {
           [[INVoiceShortcutCenter sharedCenter] getAllVoiceShortcutsWithCompletion:^(NSArray<INVoiceShortcut *> * _Nullable voiceShortcuts, NSError * _Nullable error) {
               
               dispatch_async(dispatch_get_main_queue(), ^{
                   BOOL tempAddedShortcut = NO;
                   for (INVoiceShortcut *voiceShortcut in voiceShortcuts) {
                       NSLog(@"voiceShortcut.identifier = %@",voiceShortcut.identifier);
                       NSLog(@"voiceShortcut.invocationPhrase = %@",voiceShortcut.invocationPhrase);
                       NSLog(@"voiceShortcut.shortcut = %@",voiceShortcut.shortcut.userActivity.title);
                       NSLog(@"voiceShortcut.shortcut = %@",voiceShortcut.shortcut.userActivity.userInfo);

                       
                       if ([voiceShortcut.shortcut.intent isKindOfClass:[SiriIntent class]]) {
                           tempAddedShortcut = YES;
//                           break;
                       }
                   }
                    if (tempAddedShortcut) {
                        
                       INUIEditVoiceShortcutViewController *editVoiceShortcutViewController = [[INUIEditVoiceShortcutViewController alloc] initWithVoiceShortcut:voiceShortcuts[0]];
                       editVoiceShortcutViewController.delegate = self;
                       [self presentViewController:editVoiceShortcutViewController animated:YES completion:nil];
                   } else {
                      INShortcut *shortCut = [[INShortcut alloc] initWithIntent:self.testIntent];
                        
                          self.customShortCutViewController = [[INUIAddVoiceShortcutViewController alloc] initWithShortcut:shortCut];
                           self.customShortCutViewController.delegate = self;
                          [self presentViewController:self.customShortCutViewController animated:YES completion:nil];
                   }
               });
           }];
       }
   }

-(void)addVoiceShortcutViewControllerDidCancel:(INUIAddVoiceShortcutViewController *)controller
{
    [controller dismissViewControllerAnimated:YES completion:nil];
}

-(void)addVoiceShortcutViewController:(INUIAddVoiceShortcutViewController *)controller didFinishWithVoiceShortcut:(INVoiceShortcut *)voiceShortcut error:(NSError *)error
{
    [controller dismissViewControllerAnimated:YES completion:nil];
}
 
- (void)editVoiceShortcutViewController:(INUIEditVoiceShortcutViewController *)controller didUpdateVoiceShortcut:(nullable INVoiceShortcut *)voiceShortcut error:(nullable NSError *)error{
    [controller dismissViewControllerAnimated:YES completion:nil];

}

/*!
 @abstract Called if the user deletes the voice shortcut.
 @discussion Your implementation of this method should dismiss the view controller.
 */
- (void)editVoiceShortcutViewController:(INUIEditVoiceShortcutViewController *)controller didDeleteVoiceShortcutWithIdentifier:(NSUUID *)deletedVoiceShortcutIdentifier{
    [controller dismissViewControllerAnimated:YES completion:nil];

}
 
 */
- (void)editVoiceShortcutViewControllerDidCancel:(INUIEditVoiceShortcutViewController *)controller{
    [controller dismissViewControllerAnimated:YES completion:nil];

}
 


@end

这一小部分可以判断本地是否已经添加了快捷方式,如果添加就需要进入编辑页面,如果没有添加进入到添加页面

 [[INVoiceShortcutCenter sharedCenter] getAllVoiceShortcutsWithCompletion:^(NSArray<INVoiceShortcut *> * _Nullable voiceShortcuts, NSError * _Nullable error) {
               
               dispatch_async(dispatch_get_main_queue(), ^{
                   BOOL tempAddedShortcut = NO;
                   for (INVoiceShortcut *voiceShortcut in voiceShortcuts) {
                       NSLog(@"voiceShortcut.identifier = %@",voiceShortcut.identifier);
                       NSLog(@"voiceShortcut.invocationPhrase = %@",voiceShortcut.invocationPhrase);
                       NSLog(@"voiceShortcut.shortcut = %@",voiceShortcut.shortcut.userActivity.title);
                       NSLog(@"voiceShortcut.shortcut = %@",voiceShortcut.shortcut.userActivity.userInfo);

                       
                       if ([voiceShortcut.shortcut.intent isKindOfClass:[SiriIntent class]]) {
                           tempAddedShortcut = YES;
//                           break;
                       }
                   }
                    if (tempAddedShortcut) {
                        
                       INUIEditVoiceShortcutViewController *editVoiceShortcutViewController = [[INUIEditVoiceShortcutViewController alloc] initWithVoiceShortcut:voiceShortcuts[0]];
                       editVoiceShortcutViewController.delegate = self;
                       [self presentViewController:editVoiceShortcutViewController animated:YES completion:nil];
                   } else {
                      INShortcut *shortCut = [[INShortcut alloc] initWithIntent:self.testIntent];
                        
                          self.customShortCutViewController = [[INUIAddVoiceShortcutViewController alloc] initWithShortcut:shortCut];
                           self.customShortCutViewController.delegate = self;
                          [self presentViewController:self.customShortCutViewController animated:YES completion:nil];
                   }
               });

最重要的一点 开启siri权限,而且需要具有开发者证书才可以开始siri,自己的没有缴费的开发者证书是开启不了的
,然后在主项目的info.list中添加
Privacy - Siri Usage Description 权限

截屏2020-12-18 下午2.44.56.png
上一篇下一篇

猜你喜欢

热点阅读