关于siri的新特性-Shortcuts(oc版)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_28585351/article/details/82885926

因为官方Demo是swift版,学习之前整理出了个oc版(看来有时间要把swift捡起来了...)

苹果官方Demo: https://developer.apple.com/documentation/sirikit/soup_chef_accelerating_app_interactions_with_shortcuts?language=objc

首先添加intent文件

然后导入框架

接下来就可以直接做了,引入头文件,初始化相关控件

#import <Intents/Intents.h>
#import "intentIntent.h"
#import <IntentsUI/IntentsUI.h>

//同时要遵守协议 
 <INUIAddVoiceShortcutButtonDelegate, INUIAddVoiceShortcutViewControllerDelegate, INUIEditVoiceShortcutViewControllerDelegate>

@property (nonatomic, strong) IntentIntent *intent;
@property (strong, nonatomic) INUIAddVoiceShortcutButton API_AVAILABLE(ios(12.0)) *shortcutButton;



- (IntentIntent *)intent API_AVAILABLE(ios(12.0)){
    if(!_intent){
        _intent = [[IntentIntent alloc] init];
        _intent.suggestedInvocationPhrase = @"raymon~";
    }
    return _intent;
}

- (INUIAddVoiceShortcutButton *)shortcutButton API_AVAILABLE(ios(12.0)){
    if (!_shortcutButton) {
        _shortcutButton = [[INUIAddVoiceShortcutButton alloc] initWithStyle:INUIAddVoiceShortcutButtonStyleWhiteOutline];
        _shortcutButton.shortcut = [[INShortcut alloc] initWithIntent:self.intent];
        _shortcutButton.translatesAutoresizingMaskIntoConstraints = false;
        _shortcutButton.delegate = self;
    }
    return _shortcutButton;
}

实现协议方法

#pragma mark - INUIAddVoiceShortcutButtonDelegate
- (void)presentAddVoiceShortcutViewController:(INUIAddVoiceShortcutViewController *)addVoiceShortcutViewController forAddVoiceShortcutButton:(INUIAddVoiceShortcutButton *)addVoiceShortcutButton API_AVAILABLE(ios(12.0)){
    addVoiceShortcutViewController.delegate = self;
    [self presentViewController:addVoiceShortcutViewController animated:YES completion:nil];
}

- (void)presentEditVoiceShortcutViewController:(INUIEditVoiceShortcutViewController *)editVoiceShortcutViewController forAddVoiceShortcutButton:(INUIAddVoiceShortcutButton *)addVoiceShortcutButton API_AVAILABLE(ios(12.0)){
    editVoiceShortcutViewController.delegate = self;
    [self presentViewController:editVoiceShortcutViewController animated:YES completion:nil];
}

#pragma mark - INUIAddVoiceShortcutViewControllerDelegate
- (void)addVoiceShortcutViewController:(INUIAddVoiceShortcutViewController *)controller didFinishWithVoiceShortcut:(INVoiceShortcut *)voiceShortcut error:(NSError *)error
API_AVAILABLE(ios(12.0)) API_AVAILABLE(ios(12.0)){
    NSLog(@"addVoiceShortcutViewController------%@",error);
    [controller dismissViewControllerAnimated:YES completion:nil];
}

- (void)addVoiceShortcutViewControllerDidCancel:(INUIAddVoiceShortcutViewController *)controller API_AVAILABLE(ios(12.0)){
    [controller dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - INUIEditVoiceShortcutViewControllerDelegate
- (void)editVoiceShortcutViewControllerDidCancel:(INUIEditVoiceShortcutViewController *)controller API_AVAILABLE(ios(12.0)){
    [controller dismissViewControllerAnimated:YES completion:nil];
}

- (void)editVoiceShortcutViewController:(INUIEditVoiceShortcutViewController *)controller didUpdateVoiceShortcut:(INVoiceShortcut *)voiceShortcut error:(NSError *)error API_AVAILABLE(ios(12.0)){
    NSLog(@"editVoiceShortcutViewControllerDidCancel-----%@",error);
    [controller dismissViewControllerAnimated:YES completion:nil];
}

- (void)editVoiceShortcutViewController:(INUIEditVoiceShortcutViewController *)controller didDeleteVoiceShortcutWithIdentifier:(NSUUID *)deletedVoiceShortcutIdentifier API_AVAILABLE(ios(12.0)){
    [controller dismissViewControllerAnimated:YES completion:nil];
}

在APPDelegate中实现方法

- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray<id<UIUserActivityRestoring>> * _Nullable))restorationHandler{
    NSLog(@"continueUserActivity");
    if ([userActivity.activityType isEqualToString:@"daysign.siriShortcut.type"]) {
        NSLog(@"------------要干嘛");
    }
    return YES;
}

差点忘记,大家也要注意的地方,那就是在info.plist中添加相关的key和value(这里的daysign.siriShortcut.type就是上面appdelegate中用到的参数)

这样就可以实现siri shortcut快捷语设置了

猜你喜欢

转载自blog.csdn.net/sinat_28585351/article/details/82885926
今日推荐