iOS12 最新系统,可以使用Siri捷径了,现将创建步骤记录如下
先看一下效果吧。GitHub地址
1 创建Custom Intent。command + N 快捷键 新建一个 Intents.intentdefinition文件。这个文件用来定义自定义intent类型。
设置相关参数
同时会自动在项目的Info.plist文件中添加NSUserActivityTypes
在项目的Build Phases中的Link Binary With Libraries中添加Intents.framework和IntentsUI.framework
代码如下
APPDelegate.M文件如下
- (BOOL)application:(UIApplication*)application continueUserActivity:(NSUserActivity*)userActivity restorationHandler:(void(^)(NSArray> *_Nullable))restorationHandler
{
// activityType 和 Info.plist中的NSUserActivityTypes内容对应
if ([userActivity.activityType isEqualToString:@"SiriDemoIntent"]) {
// Siri接收到命令,打开本APP,做出相应的处理即可
}
return YES;
}
ViewController.M文件如下
#import "ViewController.h"
#import "Intents/Intents.h"
#import "IntentsUI/IntentsUI.h"
#import "SiriDemoIntent.h"
// 设置Custom Intents”图中右边箭头指的“Class Name”
@interface ViewController ()<INUIAddVoiceShortcutButtonDelegate,INUIAddVoiceShortcutViewControllerDelegate,INUIEditVoiceShortcutViewControllerDelegate>
@property (nonatomic, strong) SiriDemoIntent API_AVAILABLE(ios(12.0)) *intent;
@property(nonatomic,strong)INUIAddVoiceShortcutButtonAPI_AVAILABLE(ios(12.0)) *shortcutButton;
@property (nonatomic,assign) BOOL addedShortcut;
@property (nonatomic,strong)UIButton* tempBtn;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
/* 系统自带按钮 是一个Siri图片和 add to siri 英文
if (@available(iOS 12.0, *)) {
_shortcutButton = [[INUIAddVoiceShortcutButton alloc] initWithStyle:INUIAddVoiceShortcutButtonStyleWhiteOutline];
_shortcutButton.shortcut = [[INShortcut alloc] initWithIntent:self.intent];
_shortcutButton.translatesAutoresizingMaskIntoConstraints = false;
_shortcutButton.delegate = self;
[self.view addSubview:_shortcutButton];
}
*/
//自定义按钮
UIButton *tempBtn = [UIButton buttonWithType:UIButtonTypeCustom];
tempBtn.frame=CGRectMake(100,100,100,50);
[tempBtnsetTitle:@"添加Siri" forState:UIControlStateNormal];
tempBtn.titleLabel.font = [UIFont systemFontOfSize:15];
[tempBtnsetTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
tempBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[tempBtnaddTarget:self action:@selector(shortcutButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
self.tempBtn= tempBtn;
[self.viewaddSubview:tempBtn];
}
- (void)shortcutButtonClicked:(UIButton*)sender
{
if(@available(iOS12.0, *)) {
[[INVoiceShortcutCenter sharedCenter] getAllVoiceShortcutsWithCompletion:^(NSArray<INVoiceShortcut *> * _Nullable voiceShortcuts, NSError * _Nullable error) {
dispatch_async(dispatch_get_main_queue(), ^{
BOOLtempAddedShortcut =NO;
for(INVoiceShortcut*voiceShortcutinvoiceShortcuts) {
if([voiceShortcut.shortcut.intentisKindOfClass:[SiriDemoIntentclass]]) {
tempAddedShortcut =YES;
break;
}
}
self.addedShortcut= tempAddedShortcut;
if(self.addedShortcut) {
INUIEditVoiceShortcutViewController *editVoiceShortcutViewController = [[INUIEditVoiceShortcutViewController alloc] initWithVoiceShortcut:voiceShortcuts[0]];
editVoiceShortcutViewController.delegate=self;
[selfpresentViewController:editVoiceShortcutViewControlleranimated:YEScompletion:nil];
}else{
INShortcut*shortcut = [[INShortcutalloc]initWithIntent:self.intent];
INUIAddVoiceShortcutViewController *addVoiceShortcutViewController = [[INUIAddVoiceShortcutViewController alloc] initWithShortcut:shortcut];
addVoiceShortcutViewController.delegate=self;
[selfpresentViewController:addVoiceShortcutViewControlleranimated:YEScompletion:nil];
}
});
}];
}
}
- (SiriDemoIntent*)intentAPI_AVAILABLE(ios(12.0)){
if(!_intent) {
_intent = [[SiriDemoIntent alloc] init];
_intent.suggestedInvocationPhrase = @"启动SiriDemo"; //在Siri语音设置时显示的建议设置唤起文字
}
return _intent;
}
#pragma mark - INUIAddVoiceShortcutButtonDelegate
- (void)presentAddVoiceShortcutViewController:(INUIAddVoiceShortcutViewController*)addVoiceShortcutViewController forAddVoiceShortcutButton:(INUIAddVoiceShortcutButton*)addVoiceShortcutButtonAPI_AVAILABLE(ios(12.0)){
addVoiceShortcutViewController.delegate=self;
[selfpresentViewController:addVoiceShortcutViewControlleranimated:YEScompletion:nil];
}
- (void)presentEditVoiceShortcutViewController:(INUIEditVoiceShortcutViewController*)editVoiceShortcutViewController forAddVoiceShortcutButton:(INUIAddVoiceShortcutButton*)addVoiceShortcutButtonAPI_AVAILABLE(ios(12.0)){
editVoiceShortcutViewController.delegate=self;
[selfpresentViewController:editVoiceShortcutViewControlleranimated:YEScompletion:nil];
}
#pragma mark - INUIAddVoiceShortcutViewControllerDelegate
- (void)addVoiceShortcutViewController:(INUIAddVoiceShortcutViewController*)controller didFinishWithVoiceShortcut:(INVoiceShortcut*)voiceShortcut error:(NSError*)error
API_AVAILABLE(ios(12.0)){
if(error ==nil){
[self.tempBtn setTitle:@"添加成功" forState:UIControlStateNormal];
}
[controllerdismissViewControllerAnimated:YES completion:nil];
}
- (void)addVoiceShortcutViewControllerDidCancel:(INUIAddVoiceShortcutViewController*)controllerAPI_AVAILABLE(ios(12.0)){
[controllerdismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - INUIEditVoiceShortcutViewControllerDelegate
- (void)editVoiceShortcutViewControllerDidCancel:(INUIEditVoiceShortcutViewController*)controllerAPI_AVAILABLE(ios(12.0)){
[controllerdismissViewControllerAnimated:YES completion:nil];
}
- (void)editVoiceShortcutViewController:(INUIEditVoiceShortcutViewController*)controller didUpdateVoiceShortcut:(INVoiceShortcut*)voiceShortcut error:(NSError*)errorAPI_AVAILABLE(ios(12.0)){
if(error ==nil){
[self.tempBtn setTitle:@"修改成功" forState:UIControlStateNormal];
}
[controllerdismissViewControllerAnimated:YES completion:nil];
}
- (void)editVoiceShortcutViewController:(INUIEditVoiceShortcutViewController*)controller didDeleteVoiceShortcutWithIdentifier:(NSUUID*)deletedVoiceShortcutIdentifierAPI_AVAILABLE(ios(12.0)){
[self.tempBtn setTitle:@"Siri" forState:UIControlStateNormal];
[controllerdismissViewControllerAnimated:YES completion:nil];
}
- (void)addMenuItemShortcuts
{
//如果没有在APP内添加Siri,用此方法可以在设置-Siri搜索建议中找到该APP 并添加
if(@available(iOS12.0, *)){
SiriDemoIntent *intent = [[SiriDemoIntent alloc] init];
intent.suggestedInvocationPhrase = NSLocalizedString(@"SIRI_SHORTCUT_CORRECT_WORK", nil);
[[INVoiceShortcutCenter sharedCenter] setShortcutSuggestions:@[[[INShortcut alloc] initWithIntent:intent]]];
}
}
@end
网友评论