JLChen
2021-11-29 06c09ecbdf83cc5cc33971ffb75ba81e85b6eb33
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//
//  HDLSceneHandler.m
//  HDLSceneSiri
//
//  Created by 陈嘉乐 on 2021/11/25.
//
 
#import "HDLSceneHandler.h"
 
@implementation HDLSceneHandler
 
// 该方法是在 siri 匹配到相应的 Intent 时候调用。
// 如果用失败的方法初始化HDLRunSceneIntentResponse,在匹siri后就会报错
// 不管用哪个成功的方式,只是初始化参数的值而已,都一样
// 此为第一道拦截,如果code设置为失败或者打开app,那就直接失败/跳走了,连展示界面的机会都没有
// 所以一半这里就直接返回成功,让siri继续往下走,打开界面
// 建议:只用reday和success 这两个code
- (void)confirmRunScene:(HDLRunSceneIntent *)intent completion:(void (^)(HDLRunSceneIntentResponse *response))completion NS_SWIFT_NAME(confirm(intent:completion:)){
    
   
    
    HDLRunSceneIntentResponse *rsp = [[HDLRunSceneIntentResponse alloc] initWithCode:HDLRunSceneIntentResponseCodeInProgress userActivity:nil];
    rsp.successMessage =@"请等待...";
    completion(rsp);
}
 
- (void)handleHDLRunScene:(nonnull HDLRunSceneIntent *)intent completion:(nonnull void (^)(HDLRunSceneIntentResponse * _Nonnull))completion {
    NSLog(@"HDL handleRunScene");
    NSString *text = @"执行成功";
  
 
    NSData *jsonData = [intent.controlJSONStr dataUsingEncoding:NSUTF8StringEncoding];
    NSError *error;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
    if (error) {
      //解析出错
    }
    NSLog(@"dic :%@",dic);
    text = [NSString stringWithFormat:@"%@ %@",text,intent.controlJSONStr];
 
    
    //执行成功
    HDLRunSceneIntentResponse *rsp = [[HDLRunSceneIntentResponse alloc] initWithCode:HDLRunSceneIntentResponseCodeSuccess userActivity:nil];
    rsp.controlName = intent.controlName;
    rsp.successMessage = text;
    completion(rsp);
    
}
 
 
@end