iOS Siri Kit 添加一個自己的快捷指令

第一步 創建一個 Intents 選擇siriKit

5BC4E856-D403-457F-8CA4-C47D929EB798.png
接下來 配置這個 Intents
91A65B42-ECED-4A0A-AC5D-389EBC83AC3C.png

FA3B0D48-D9B7-4041-8C61-010B3F099191.png

A08D2E3B-BF73-46B8-A2A6-22AD34C47357.png

99F89395-DBDB-4AB4-ACBF-40C78D0E5055.png
到這 前期的準備工作完成

第二步

開始 使用代碼將指令添加到Siri內
在綁定的界面中添加 這兩個系統類 和 剛才自定義名稱的Intent
#import <IntentsUI/IntentsUI.h>
#import <Intents/Intents.h>
#import "GJCustomIntent.h"
@property (nonatomic, strong) GJCustomIntentAPI_AVAILABLE(ios(12.0)) *intent;
@property (nonatomic, strong) INUIAddVoiceShortcutButtonAPI_AVAILABLE(ios(12.0)) *shortcutButton;

//懶加載 這里的title 和 sceneID 就是上面創建的兩個字段,用于告訴siri這個綁定的指令是什么,方便當Siri觸發后,再將這個字段返回給我們,讓我們做業務處理
- (GJCustomIntent *)intentAPI_AVAILABLE(ios(12.0)){
    if (!_intent) {
        _intent = [[GJCustomIntentalloc] init];
        _intent.title = @"自定義Title";
        _intent.sceneID = @"123456”;
//在Siri語音設置時顯示的建議設置喚起文字
        _intent.suggestedInvocationPhrase = @"開始執行";           
    }
    return_intent;
}
再需要觸發的地方添加 系統的指令Button
 if (@available(iOS 12.0, *)) {
        _shortcutButton = [[INUIAddVoiceShortcutButtonalloc] initWithStyle:INUIAddVoiceShortcutButtonStyleWhiteOutline];
      
        _shortcutButton.shortcut = [[INShortcutalloc] initWithIntent:self.intent];
        _shortcutButton.translatesAutoresizingMaskIntoConstraints = false;
        _shortcutButton.delegate = self;
        [self.colorViewaddSubview:_shortcutButton];
  }
添加指令觸發的代理方法
< INUIAddVoiceShortcutButtonDelegate,
INUIAddVoiceShortcutViewControllerDelegate,
INUIEditVoiceShortcutViewControllerDelegate>

#pragma mark - INUIAddVoiceShortcutButtonDelegate
- (void)presentAddVoiceShortcutViewController:(INUIAddVoiceShortcutViewController *)addVoiceShortcutViewController forAddVoiceShortcutButton:(INUIAddVoiceShortcutButton *)addVoiceShortcutButton API_AVAILABLE(ios(12.0)){
    addVoiceShortcutViewController.delegate = self;
    [selfpresentViewController:addVoiceShortcutViewController animated:YEScompletion:nil];
}
- (void)presentEditVoiceShortcutViewController:(INUIEditVoiceShortcutViewController *)editVoiceShortcutViewController forAddVoiceShortcutButton:(INUIAddVoiceShortcutButton *)addVoiceShortcutButton API_AVAILABLE(ios(12.0)){
    editVoiceShortcutViewController.delegate = self;
    [selfpresentViewController:editVoiceShortcutViewController animated:YEScompletion:nil];
}
#pragma mark - INUIAddVoiceShortcutViewControllerDelegate
- (void)addVoiceShortcutViewController:(INUIAddVoiceShortcutViewController *)controller didFinishWithVoiceShortcut:(INVoiceShortcut *)voiceShortcut error:(NSError *)error
API_AVAILABLE(ios(12.0)){
    [controller dismissViewControllerAnimated:YEScompletion:nil];
}
- (void)addVoiceShortcutViewControllerDidCancel:(INUIAddVoiceShortcutViewController *)controller API_AVAILABLE(ios(12.0)){
    [controller dismissViewControllerAnimated:YEScompletion:nil];
}
#pragma mark - INUIEditVoiceShortcutViewControllerDelegate
- (void)editVoiceShortcutViewControllerDidCancel:(INUIEditVoiceShortcutViewController *)controller API_AVAILABLE(ios(12.0)){
    [controller dismissViewControllerAnimated:YEScompletion:nil];
}
- (void)editVoiceShortcutViewController:(INUIEditVoiceShortcutViewController *)controller didUpdateVoiceShortcut:(INVoiceShortcut *)voiceShortcut error:(NSError *)error API_AVAILABLE(ios(12.0)){
    [controller dismissViewControllerAnimated:YEScompletion:nil];
}
- (void)editVoiceShortcutViewController:(INUIEditVoiceShortcutViewController *)controller didDeleteVoiceShortcutWithIdentifier:(NSUUID *)deletedVoiceShortcutIdentifier API_AVAILABLE(ios(12.0)){
    [controller dismissViewControllerAnimated:YEScompletion:nil];
}
添加完這些后我們就可以調起 系統的添加到指令界面了
IMG_36314F274BE1-1.jpeg

第三步

添加指令成功后,我們就要去響應我們設置的指令內容了
創建一個IntentHandler 去處理
在項目的總設置地方添加 類似添加widget
1E94FABC-B14D-43CE-A8F8-3F2269DED69C.png

23809554-0502-45E6-8D2F-3E55E9D39A84.png
創建成功后,在項目文件內就可以看到兩個新的文件
8AE4DBCA-1EBC-4949-9D3B-2D926710593E.png
因為要處理自己的業務邏輯 所以我自定義了一個Handler
BF2E59C0-C7CB-410D-B379-D1D8236C5E17.png
在.m文件中 實現兩個方法 用于自己的業務邏輯判斷

每個自定義名稱方法都會不同

// 準備階段  如果選擇了 用戶詢問的選項,那么這個就會作為第一層界面 讓siri繼續往下走,打開界面
//如果在這個方法就返回一個 IntentResponseCodeFailure 錯誤的返回,那么下面的界面都不會出現了
-(void)confirmGJCustom:(GJCustomIntent *)intent completion:(void (^)(GJCustomIntentResponse * _Nonnull))completion{
//    intent.title
//    intent.sceneID
    //第一層 判讀是否登錄等權限
//    NSUserDefaults *user =[NSUserDefaults standardUserDefaults];
//    NSString *str = [user objectForKey:@"user_name"];
   
    GJCustomIntentResponse *rsp = [[GJCustomIntentResponsealloc] initWithCode:GJCustomIntentResponseCodeReadyuserActivity:nil];
    rsp.secenID = intent.sceneID;
    rsp.title = intent.title;
    completion(rsp);
   
 
}
// 觸發階段
- (void)handleGJCustom:(nonnullGJCustomIntent *)intent completion:(nonnullvoid (^)(GJCustomIntentResponse * ))completion {
  
    GJCustomIntentResponse *rsp = [[GJCustomIntentResponsealloc] initWithCode:GJCustomIntentResponseCodeSuccessuserActivity:nil];
    rsp.secenID =  intent.sceneID;
    rsp.title = intent.title;
    completion(rsp);
}
回到接收指令的第一個入口 IntentHandler
- (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.
    if ([intent isKindOfClass:[GJCustomIntentclass]]) {
        return [[GJCustomHandleralloc]init];
       }
    returnself;
}
這里的業務邏輯就處理完成了,下面就是Siri彈出框的界面展示
進入UI界面 IntentViewController.m
#pragma mark - INUIHostedViewControlling
// Prepare your view controller for the interaction to handle.
- (void)configureViewForParameters:(NSSet <INParameter *> *)parameters ofInteraction:(INInteraction *)interaction interactiveBehavior:(INUIInteractiveBehavior)interactiveBehavior context:(INUIHostedViewContext)context completion:(void (^)(BOOL success, NSSet <INParameter *> *configuredParameters, CGSize desiredSize))completion {
//根據不同的類型展示不同的界面
 if (interaction.intentHandlingStatus ==      INIntentHandlingStatusReady) {
  } elseif (interaction.intentHandlingStatus == INIntentHandlingStatusSuccess) {
}

}
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容