Siri shortcuts 指北

系統支持iOS 12bash

工程搭建

開啓工程權限

image.png

添加新的target

選擇File → New → Target,勾選Include UI Extension,能夠使用自定義UI擴展。app

image.png

建立Intent Definition File

方法:File → New → Fileide

image.png

新建Intent

image.png

建立完後以下界面:ui

image.png

能夠看到Intent是一個Category,咱們能夠設置類型(標示Intent的做用),添加參數(根據Siri解析命令傳入),添加標題,描述(這些會顯示在Siri喚醒咱們app的時候)。this

編譯的時候系統會自動生成一個子類XXXIntent : INIntent,咱們須要找到這個類,使用這個類來進行咱們的其餘操做。spa

點擊以下圖位置:code

image.png

開發過程

權限獲取

獲取當前權限cdn

INSiriAuthorizationStatus siriStatus = [INPreferences siriAuthorizationStatus];
複製代碼

請求獲取權限視頻

[INPreferences requestSiriAuthorization:^(INSiriAuthorizationStatus status) {
                switch (status) {
                    case INSiriAuthorizationStatusAuthorized: // 成功獲取權限
                        NSLog(@"權限獲取成功");
                        break;
                    case INSiriAuthorizationStatusDenied: // 成功獲取權限
                        NSLog(@"權限獲取用戶拒絕");
                        break;
                        
                    default:
                        break;
                }
            }];
複製代碼

注意添加info.plist(或者多語言)設置提示語,不然權限請求不會彈出。blog

image.png

添加Siri快捷指令頁面

調用系統API,調用以下頁面。

image.png

代碼以下:

GotoPageIntent *intent = [[GotoPageIntent alloc] init]; // GotoPageIntent爲咱們自定義的Intent,找不到看上面
    intent.suggestedInvocationPhrase = @"打開app"; // 這是建議的提示語,會展現在頁面上
    INShortcut *shortcurt = [[INShortcut alloc] initWithIntent:intent];
    
    INUIAddVoiceShortcutViewController *addvc = [[INUIAddVoiceShortcutViewController alloc] initWithShortcut:shortcurt];
    addvc.delegate = self;
    [self presentViewController:addvc animated:YES completion:nil];
複製代碼

處理回調:

/*!
 @abstract Called after the user finishes the setup flow for the voice shortcut, with either the successfully-added voice shortcut, or an error.
 @discussion Your implementation of this method should dismiss the view controller.
 */
- (void)addVoiceShortcutViewController:(INUIAddVoiceShortcutViewController *)controller didFinishWithVoiceShortcut:(nullable INVoiceShortcut *)voiceShortcut error:(nullable NSError *)error; {
    if (!error) {
        [controller dismissViewControllerAnimated:YES completion:nil];
    }
}

/*!
 @abstract Called if the user cancels the setup flow; the voice shortcut was not added.
 @discussion Your implementation of this method should dismiss the view controller.
 */
- (void)addVoiceShortcutViewControllerDidCancel:(INUIAddVoiceShortcutViewController *)controller; {
    [controller dismissViewControllerAnimated:YES completion:nil];
}
複製代碼

處理Siri Shortcuts觸發的回調

  • 自定義xxxIntentHandler,繼承自NSObject。遵循xxxIntentHandling(這個是咱們Xcode本身生成的,找的方法見上面建立Intent Definition File中),在裏面實現咱們須要的邏輯。
- (void)handleGotoPage:(GotoPageIntent *)intent completion:(void (^)(GotoPageIntentResponse *response))completion NS_SWIFT_NAME(handle(intent:completion:)) {
    // GotoPageIntentResponseCodeContinueInApp 打開app
    // GotoPageIntentResponseCodeSuccess 回調成功
    GotoPageIntentResponse *response = [[GotoPageIntentResponse alloc] initWithCode:GotoPageIntentResponseCodeSuccess userActivity:nil];
    completion(response);
}

複製代碼
  • 打開咱們的Extension,IntentHander方法。處理咱們本身定義的Intent。
- (id)handlerForIntent:(INIntent *)intent {
    if ([intent isKindOfClass:[GotoPageIntent class]]) {
        return [[GotoAppIntentHandler alloc] init];
    }
    
    return self;
}
複製代碼

自定義 ExtensionUI

能夠自定義Siri呼出app的樣式,在IntentViewController中開發。

參考資料

WWDC視頻:

developer.apple.com/videos/play…

蘋果Demo地址:

developer.apple.com/documentati…

文檔: developer.apple.com/documentati…

相關文章
相關標籤/搜索