直接開始
第一步: 添加文件Sirikit Intent Definition File
第二步:編輯文件
選擇處理類別 generic 是直接運(yùn)行 項(xiàng)目,其他的需要操作
如果選擇了這個(gè)按鈕也是需要點(diǎn)擊后才開始執(zhí)行,不選擇則會(huì)直接運(yùn)行
這里添加你想要跟業(yè)務(wù)相關(guān)的字段
在這里直接設(shè)置 可以在添加快捷指令頁面顯示
編譯運(yùn)行绳泉,會(huì)在右側(cè)編輯欄看到
siriintent 就是你接下來需要用到的類
第二步:
添加intent ,創(chuàng)建intent 時(shí)候會(huì)提示是否同時(shí)創(chuàng)建intent UI疯趟,如果需要頁面展示選擇是
這塊就是你創(chuàng)建好之后的文件目錄
網(wǎng)上大多在講解在intentHandler類中 導(dǎo)入你的intent文件頭文件虏辫,你會(huì)發(fā)現(xiàn)此時(shí)會(huì)報(bào)頭文件找不到
注意:此時(shí)你就需要在targets-》build phases 中添加第一步你所創(chuàng)建的intentdefinition文件倘要,這樣你在intent文件導(dǎo)入頭文件時(shí)褐筛,就不會(huì)報(bào)錯(cuò)了类少,intent UI 中也需要這么做
第三步,代碼處理
在intenthandler中根據(jù)intent業(yè)務(wù)類型不同判斷處理邏輯
- (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]);
}
}
}
在這個(gè)類中 添加你的業(yè)務(wù)邏輯
再次注意: 如果你的的業(yè)務(wù)邏輯也需要涉及到http請(qǐng)求瞒滴,在 intent UI或者 intent 中的 info.plist文件中 添加
Allow Arbitrary Loads ---- yes
允許http請(qǐng)求
第五步曲梗,開始在你的主項(xiàng)目使用siri shourtcuts的地方添加調(diào)起代碼
//
// 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(@"用戶尚未對(duì)該應(yīng)用程序作出選擇赞警。");
break;
case INSiriAuthorizationStatusRestricted:
NSLog(@"此應(yīng)用程序無權(quán)使用Siri服務(wù)");
break;
case INSiriAuthorizationStatusDenied:
NSLog(@"用戶已明確拒絕此應(yīng)用程序的授權(quán)");
break;
case INSiriAuthorizationStatusAuthorized:
NSLog(@"用戶可以使用此應(yīng)用程序的授權(quán)");
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
這一小部分可以判斷本地是否已經(jīng)添加了快捷方式,如果添加就需要進(jìn)入編輯頁面虏两,如果沒有添加進(jìn)入到添加頁面
[[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];
}
});
最重要的一點(diǎn) 開啟siri權(quán)限愧旦,而且需要具有開發(fā)者證書才可以開始siri,自己的沒有繳費(fèi)的開發(fā)者證書是開啟不了的
定罢,然后在主項(xiàng)目的info.list中添加
Privacy - Siri Usage Description 權(quán)限