本文介紹了使用OC如何構建帶有Shortcuts的專案.
轉載請註明出處:juejin.im/post/5b51b4…
Shortcuts其初衷是為了讓使用者通過Siri使用一句簡短的話來實現一些複雜操作.舉個栗子就像是你給了Siri一個眼神,然後Siri就知道你要幹嘛了.
建立Shortcuts的基本流程
1.宣告Shortcuts(Define Shortcut)
2.配置Shortcuts(Donate Shortcut)
3.接收Shortcuts的回撥(Handle Shortcut)
Shortcuts APIs
Shortcuts提供了2種模型,用於在Siri中接收與傳遞相關資訊.

NSUserActivity | Intents |
---|---|
需要開啟app進行操作時使用 | 需要在Siri介面直接操作時使用 |
僅僅表示在Spotlight中的索引專案時 | 需要對操作新增自定義短語或自定義UI時 |
Siri建議的顆粒度較大 | Siri會根據不同情況給出相對精確的建議 |
NSUserActivity使用方法:
1.在info.plist中新增NSUserActivityTypes

NSUserActivity *userActivity = [[NSUserActivity alloc] initWithActivityType:@"com.js.sirishortcuts-activity-type"];
userActivity.eligibleForSearch = YES;
userActivity.title = @"這是標題";
if (@available(iOS 12.0, *)) {
userActivity.eligibleForPrediction = YES;// 新屬性,賦值為 true 後可以暴露給 `SiriKit`
userActivity.suggestedInvocationPhrase = @"叫車回家";//搜尋的關鍵字
}
CSSearchableItemAttributeSet * attributes = [[CSSearchableItemAttributeSet alloc] init];
UIImage *icon = [UIImage imageNamed:@"main_icon_jinbi_layer_bao02"];
attributes.thumbnailData = UIImagePNGRepresentation(icon);
attributes.contentDescription = @"這是副標題";
userActivity.contentAttributeSet = attributes;
self.userActivity = userActivity;//self 為controller
複製程式碼
執行程式碼,在搜尋頁面即可看到:(請自動忽略app名稱...)

3.在appdelegate中新增回撥
- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray<id<UIUserActivityRestoring>> * _Nullable))restorationHandler {
if ([userActivity.activityType isEqualToString: @"com.js.sirishortcuts-activity-type"]) {
NSLog(@"enter");
}
return YES;
}
複製程式碼
至此,NSUserActivity新增完成~
Intents使用方法:
1.新增Intents檔案
File -> New File,選擇 SiriKit Intent Definition File





蘋果官方推薦的使用方法是提取一個公共的framework,方便extension和target一起使用. 如果你已經有一個公共framework,那麼建議你的程式碼生成在framework中. 否則.你需要在每一個需要使用的target中生成一遍程式碼. 新增方式如下圖(此處使用的截圖是官方給出的demo中的截圖).





#import <Foundation/Foundation.h>
#import "GoHomeIntent.h"
NS_ASSUME_NONNULL_BEGIN
@interface GoHomeIntentHandler : NSObject <GoHomeIntentHandling>
@end
NS_ASSUME_NONNULL_END
複製程式碼
在GoHomeIntentHandler.m中實現協議方法. Intent 的生命週期分為:(解析和糾錯收件人和訊息內容)Resolve - (確認使用者的操作)Confirm - (將訊息傳送出去)Handle
@implementation GoHomeIntentHandler
- (void)confirmGoHome:(GoHomeIntent *)intent completion:(void (^)(GoHomeIntentResponse * _Nonnull))completion {
NSLog(@"confirmGoHome");
}
- (void)handleGoHome:(nonnull GoHomeIntent *)intent completion:(nonnull void (^)(GoHomeIntentResponse * _Nonnull))completion {
NSLog(@"handleGoHome");
GoHomeIntentResponse *response = [GoHomeIntentResponse successIntentResponseWithTime:intent.time ?: @"" location:intent.location ?: @"" price:@""];
completion(response);
}
@end
複製程式碼
在IntentHeader.m檔案中,自定義用於接收的類
- (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.
return [[GoHomeIntentHandler alloc] init];
//return self;
}
複製程式碼
4.Donate Shortcuts
- (void)gotoAddCustomVoiceShortcutView {
GoHomeIntent *intent = [[GoHomeIntent alloc] init];
intent.location = @"朝陽門東路";
intent.time = @"下午5:00";
intent.suggestedInvocationPhrase = @"下午回家";//提示使用者
INUIAddVoiceShortcutViewController *vc = [[INUIAddVoiceShortcutViewController alloc] initWithShortcut:[[INShortcut alloc] initWithIntent:intent]];
vc.delegate = self;
[self presentViewController:vc animated:YES completion:^{
;
}];
//修改
// INUIEditVoiceShortcutViewController *vc = [[INUIEditVoiceShortcutViewController alloc] initWithVoiceShortcut:voiceShortcut];
// vc.delegate = self;
// [self presentViewController:vc animated:YES completion:^{
// ;
// }];
}
複製程式碼
執行新增即可看到Siri新增自定義短語的頁面.點選紅色按鈕錄音. 從下圖中可以看出.我們之前輸入的變數已經被之前instents中設定的模板轉為對應的文案了.

刪除 donate
1.NSUserActivity
[NSUserActivity deleteSavedUserActivitiesWithPersistentIdentifiers:@[@"com.js.sirishortcuts-activity-type"] completionHandler:^{
}];
[NSUserActivity deleteAllSavedUserActivitiesWithCompletionHandler:^{
}];
複製程式碼
2.Shortcuts
[INInteraction deleteInteractionsWithIdentifiers:@[@"ur id"] completion:^(NSError * _Nullable error) {
;
}];
[INInteraction deleteAllInteractionsWithCompletion:^(NSError * _Nullable error) {
;
}];
[INInteraction deleteInteractionsWithGroupIdentifier:@"ur group id" completion:^(NSError * _Nullable error) {
;
}];
複製程式碼
測試
為了便於除錯,iOS12為我們提供了在鎖屏介面和soptlight搜尋介面顯示上次Shortcuts命令的除錯選項.不需要一遍一遍對Siri說指令了.
設定 -> 開發者-> SHORTCUTS TESTTING


OTHER
Siri ShortCuts 的推薦邏輯
Siri會根據使用者的使用習慣.在鎖屏或者搜尋頁面給出推薦的選項.並且會根據你使用時間的增加和使用習慣的改變進行改變. 這裡根據WWDC 211和其官方給出的demo舉例
- NSUserActivity
如圖,每天在相似的時間選擇相似的選項,變數數為1,此時系統會根據次數較多的進行推薦. 但是往往事件的變數不止一個.拿官方demo舉例,湯的變數有商品名,數量,選擇時滾動的距離等.


- Intens
Intens在設定的時候會提供統一的模板來設定變數. 將模型抽象出三種情況.每次統計會根據這三種情況綜合分析.




推薦的使用規則.
蘋果建議我們使用如下的規則引導使用者使用Siri ShortCuts:
1.儲存我們對Siri ShortCuts的操作結果.使我們的操作與系統儲存的結果一致.
2.避免使用時間戳等變數.
3.避免引數之間可能存在的隱式依賴關係.
另外如開篇所說.Siri ShortCuts的設計初衷是讓使用者使用簡短的一個短語來減少複雜的操作.
初來乍到.難免有些錯誤.如果有任何疑問歡迎留言指正~
參考
WWDC 211
WWDC 214
WWDC 2018: Shortcuts 快速入門
iOS12 Siri ShortCuts 應用 (一)
WWDC 2018:初識Siri Shortcuts
Apple Siri接入開發 (一)