我的代码是:
// 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 时遇到了问题,因为它无法启动。
有人对此有解决方案吗?