什么是Sirishortcut
Siri Shortcuts 的核心是自动化。
引用喵神的表述:
和 ML 领域类似,另一个开花结果的案子是对 Workflow 的收购:Shortcuts app 整个就是 Siri 版的 Workflow。Siri 在面对 Amazon 和 Google 这样的“后来者”挑战时,确实显得力不从心,而从 Keynote 的演示来看,Shortcuts 可能会是一剂良药。
和近几年的其他很多功能一样,开发者通过操作 NSUserActivity 来配置简单的 Shortcuts 以获取 Siri 的推荐。如果你想要更加自定义的行为和表现,那还需要定义和开发新的 Intent app extension。这部分内容都被作为 SiriKit 的追加内容,希望能够为用户带来每天使用 Siri 的理由吧。
Sirishortcut的使用
Sirishortcut的创建方式分为两种:
- 一种是使用NSUserActivity
- 另一种是使用Intent实现.
通过NSUserActivity创建捷径
首先需要在info.plist中添加相应的type 如下图 item0为自定义的type
注册完自定义的type之后,下面就开始创建NSUserActivity 这里一般称为donate shortcuts
我们想要在用户点击再来一单的时候,创建快捷下单的捷径:
///MARK:donate shortcuts
- (void)addCreateOrderActivity {
//根据plist文件的值,创建 UserActivity
NSUserActivity *checkInActivity = [[NSUserActivity alloc] initWithActivityType:@"com.xxxx.xxxx.orderquilkly"];
//设置 YES,通过系统的搜索,可以搜索到该 UserActivity
checkInActivity.eligibleForSearch = YES;
//允许系统预测用户行为,并在合适的时候给出提醒。(搜索界面,屏锁界面等。)
if (@available(iOS 12.0, *)) {
checkInActivity.eligibleForPrediction = YES;
}
checkInActivity.title = @"咖啡快捷下单";
checkInActivity.userInfo = @{@"key1":@"value1"};
checkInActivity.keywords = [NSSet setWithObjects:@"咖啡",@"下单", nil];
//引导用户新建语音引导(具体效果见下图)
if (@available(iOS 12.0, *)) {
checkInActivity.suggestedInvocationPhrase = @"咖啡下单";
}
CSSearchableItemAttributeSet * attributes = [[CSSearchableItemAttributeSet alloc] init];
UIImage *icon = [UIImage imageNamed:@"about_us_logo"];
attributes.thumbnailData = UIImagePNGRepresentation(icon);
attributes.contentDescription = @"每天一杯咖啡,Lucy in coffee";
checkInActivity.contentAttributeSet = attributes;
self.userActivity = checkInActivity;
}
这样用户就可以通过Spotlight搜索到下单的捷径,打开APP后在AppDelegate中的处理如下:
- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray<id<UIUserActivityRestoring>> * _Nullable))restorationHandler {
NSLog(@"%s",__func__);
NSLog(@"%@",userActivity.userInfo);
if ([userActivity.activityType isEqualToString:@"com.xxxx.xxxx.orderquilkly"]) {
NSLog(@"====>>>快捷下单处理");
} else if ([userActivity.activityType isEqualToString:@"QuickOrderCoffeeIntent"]) {
NSLog(@"===>>>自定义 intent:%@",userActivity)
}
return YES;
}
我们可以将捷径添加到Siri,引导用户设置自定义的短语,这样就通过Siri激活捷径,引导用户添加代码如下:
///MARK:建议用户添加shortcuta到Siri短语
- (void)suggestUserAddShortcutToSiri {
if (@available(iOS 12.0, *)) {
[[INVoiceShortcutCenter sharedCenter] getAllVoiceShortcutsWithCompletion:^(NSArray<INVoiceShortcut *> * _Nullable voiceShortcuts, NSError * _Nullable error) {
BOOL isAdd = NO;
INVoiceShortcut *shortcut = nil;
for (INVoiceShortcut *voice in voiceShortcuts) {
NSLog(@"====>>获取到的VoiceShortcuts :%@",voice.shortcut.userActivity.activityType);
if ([voice.shortcut.userActivity.activityType isEqualToString:@"com.xxx.xxx.orderquilkly"]) {
isAdd = YES;
shortcut = voice;
NSLog(@"===>>>快捷下单shortcut已经添加到Siri");
}
}
if (isAdd) {
[self openEditVoiceVC:shortcut];
} else {
[self openAddVoiceVC];
}
}];
}
}
///MARK:打开Siri添加短语页面
- (void)openAddVoiceVC {
if (@available(iOS 12.0, *)) {
INShortcut *shortcut = [[INShortcut alloc] initWithUserActivity:self.userActivity];
INUIAddVoiceShortcutViewController *addVc = [[INUIAddVoiceShortcutViewController alloc] initWithShortcut:shortcut];
if (!addVc.delegate) {
addVc.delegate = self;
}
@weakify(self);
[self presentViewController:addVc animated:YES completion:^{
@strongify(self);
self.addShortcutVC = addVc;
}];
}
}
///MARK:打开Siri编辑短语页面
- (void)openEditVoiceVC:(INVoiceShortcut *)voiceShortcut API_AVAILABLE(ios(12.0)){
if (@available(iOS 12.0, *)) {
INShortcut *shortcut = [[INShortcut alloc] initWithUserActivity:self.userActivity];
INUIEditVoiceShortcutViewController *editVC = [[INUIEditVoiceShortcutViewController alloc] initWithVoiceShortcut:voiceShortcut];
if (!editVC.delegate) {
editVC.delegate = self;
}
@weakify(self);
[self presentViewController:editVC animated:YES completion:^{
@strongify(self);
self.editShortcutVC = editVC;
}];
}
}
#pragma mark - INUIAddVoiceShortcutViewControllerDelegate
- (void)addVoiceShortcutViewControllerDidCancel:(INUIAddVoiceShortcutViewController *)controller
API_AVAILABLE(ios(12.0)){
NSLog(@"%s",__func__);
}
- (void)addVoiceShortcutViewController:(INUIAddVoiceShortcutViewController *)controller didFinishWithVoiceShortcut:(nullable INVoiceShortcut *)voiceShortcut error:(nullable NSError *)error API_AVAILABLE(ios(12.0)){
if (self.addShortcutVC) {
[self.addShortcutVC dismissViewControllerAnimated:YES completion:nil];
}
}
#pragma mark - INUIEditVoiceShortcutViewControllerDelegate
/*!
@abstract Called if the user updates the voice shortcut, with either the successfully-updated voice shortcut, or an error.
@discussion Your implementation of this method should dismiss the view controller.
*/
- (void)editVoiceShortcutViewController:(INUIEditVoiceShortcutViewController *)controller didUpdateVoiceShortcut:(nullable INVoiceShortcut *)voiceShortcut error:(nullable NSError *)error API_AVAILABLE(ios(12.0)){
if (self.editShortcutVC) {
[self.editShortcutVC dismissViewControllerAnimated:YES completion:nil];
}
}
通过以上代码 就可以简单的创建Sirishortcut捷径了,比较简单。下面介绍intent是方式
iOS 12 新特性 Sirishortcut(捷径)调研(二)
参考视频: