1

我的代码是:

// Create Action Set
[_myHome addActionSetWithName:@"Night" completionHandler:^(HMActionSet *actionSet, NSError *error) {
    if (error) {
        NSLog(@"%@", error);
    }else {
        NSLog(@"Add action set");
    }
}];

// Create Time Trigger
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
dateComponents.second = 5;
HMTimerTrigger *timeTrigger = [[HMTimerTrigger alloc] initWithName:@"Night Trigger" fireDate:[NSDate dateWithTimeIntervalSinceNow:5] timeZone:[NSTimeZone localTimeZone] recurrence:dateComponents recurrenceCalendar:[NSCalendar currentCalendar]];

// Add Action Set to Trigger
for (HMActionSet *actionSet in _myHome.actionSets) {
    if ([actionSet.name isEqualToString:@"Night"]) {
        [timeTrigger addActionSet:actionSet completionHandler:^(NSError *error) {
            if (error) {
                NSLog(@"%@", error);
            }else {
                NSLog(@"Add Action Set to Trigger");
            }
        }];
    }
}

// Add Trigger to My Home
[_myHome addTrigger:timeTrigger completionHandler:^(NSError *error) {
    if (error) {
        NSLog(@"%@", error);
    }else {
        NSLog(@"Add Trigger");
    }
}];

首先,我将一个名为“Night”的 HMActionSet 添加到 HMHome。然后我将相同的 HMActionSet 添加到 HMTrigger。HomeKit 抛出错误:Error Domain=HMErrorDomain Code=12 "The operation could not be completed. (HMErrorDomain error 12.)"这意味着ObjectAlreadyAssociatedToHome

我的猜测是您不能将相同的 HMActionSet 添加到 HMHome 和 HMTrigger。所以我尝试创建一个 HMActionSet 并将其添加到 HMTrigger,然后将 HMTrigger 添加到 HMHome。但是我在创建 HMActionSet 时遇到了问题,因为它无法启动。

有人对此有解决方案吗?

4

3 回答 3

1

你的问题:

dateComponents.second = 5;

计时器触发器仅在一分钟开始时设置。不使用秒数,如果触发日期包含 0 以外的秒值,则会返回错误。当计时器触发时,它通常会在预定触发日期或计算出的重复触发日期的 1 分钟内触发,具体取决于系统电源和资源管理。

根据苹果文档

于 2016-01-11T17:38:19.710 回答
0

您应该在成功添加操作集后调用添加触发器,这意味着在完成处理程序中。

于 2014-11-10T22:27:32.857 回答
0

如果 HMActionSet 已经添加到主页,则可以将其添加到 HMTrigger。

HMActionset 是一个场景(它是任何附件的可写特征的集合)。可以通过单个命令执行。

因此,任何一个都可以只执行 HMActionSet 或者可以将其添加到 Trigger 以在特定时间执行该场景。

应该有两个不同的视图来处理 HMActionSet(添加、重命名、删除、更改特征值)。

对于触发器,它将创建一个触发器并将 HMActionSet 从同一家“分配”给该触发器。

但是,您需要管理重复、触发和启用属性以完美触发。

于 2015-03-05T06:26:20.517 回答