多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
一个动作集合[HMActionSet](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMActionSet_Class/index.html#//apple_ref/occ/cl/HMActionSet)和触发器[HMTimerTrigger](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMTimerTrigger_Class/index.html#//apple_ref/occ/cl/HMTimerTrigger)允许你同时控制多个智能电器。比如,一个动作集合可能会在用户上床休息之前执行一组动作[HMAction](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMAction_Class/index.html#//apple_ref/occ/cl/HMAction)。一个写动作向一个特性写入了值。动作集合中的动作是以不确定的顺序执行的。一个触发器会在一个特定的时间出发一个动作集并可以重复执行。每一个动作集合在一个家庭中都有唯一的名称并可被Siri识别。 ![69.png](https://box.kancloud.cn/2015-08-08_55c5772469b62.png "1427352901678750.png") **创建写入动作** 写入动作会向一个服务的特性写入值并被加入到动作集合中去。[HMAction](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMAction_Class/index.html#//apple_ref/occ/cl/HMAction)类是[HMCharacteristicWriteAction](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMCharacteristicWriteAction_Class/index.html#//apple_ref/occ/cl/HMCharacteristicWriteAction)具体类的抽象基类。一个动作有一个相关联的特性对象,你可以通过Accessing Services and Characteristics中描述的来获取相关的服务和特性,然后创建这个[HMCharacteristicWriteAction](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMCharacteristicWriteAction_Class/index.html#//apple_ref/occ/cl/HMCharacteristicWriteAction)。 为了创建一个动作,我们使用[HMCharacteristicWriteAction](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMCharacteristicWriteAction_Class/index.html#//apple_ref/occ/cl/HMCharacteristicWriteAction)类中的[initWithCharacteristic:targetValue:](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMCharacteristicWriteAction_Class/index.html#//apple_ref/occ/instm/HMCharacteristicWriteAction/initWithCharacteristic:targetValue:)方法。 ~~~ HMCharacteristicWriteAction *action = [[HMCharacteristicWriteAction alloc] initWithCharacteristic:characteristic targetValue:value]; ~~~ 在你的代码中,你使用对应的特性的期望来替换value参数,并使用对应的[HMCharacteristic](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMCharacteristic_Class/index.html#//apple_ref/occ/cl/HMCharacteristic)对象来替换characteristic参数。 **创建并执行动作集** 一个动作集就是一个共同执行的动作的集合。比如一个夜间动作集合可能包含关闭电灯,调低恒温水平和锁上房门。 为了创建一个动作集我们使用[addActionSetWithName:completionHandler:](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMHome_Class/index.html#//apple_ref/occ/instm/HMHome/addActionSetWithName:completionHandler:)异步方法。 ~~~ [self.home addActionSetWithName:@"NightTime" completionHandler:^(HMActionSet *actionSet, NSError *error) { if (error == nil) { // 成功添加了一个动作集 } else { // 添加一个动作集失败 } }]; ~~~ 为了添加一个动作到动作集,我们使用[addAction:completionHandler:](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMActionSet_Class/index.html#//apple_ref/occ/instm/HMActionSet/addAction:completionHandler:)异步方法。 ~~~ [actionSet addAction:action completionHandler:^(NSError *error) { if (error == nil) { // 成功添加了一个动作到动作集 } else { // 添加一个动作到动作集失败 } }]; ~~~ 想要移除一个动作,可使用[removeAction:completionHandler:](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMActionSet_Class/index.html#//apple_ref/occ/instm/HMActionSet/removeAction:completionHandler:)方法。 想要执行一个动作集,可使用[HMHome](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMHome_Class/index.html#//apple_ref/occ/cl/HMHome)类的[executeActionSet:completionHandler:](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMHome_Class/index.html#//apple_ref/occ/instm/HMHome/executeActionSet:completionHandler:)方法。比如,用户希望控制所有的节日彩灯。我们就创建一个动作集来打开所有的节日彩灯,另外一个动作集来关闭所有的节日彩灯。为了打开所有的节日彩灯,发送executeActionSet:completionHandler:消息给home对象,并传递"打开节日彩灯"动作集。 **创建并开启触发器** 触发器会执行一个或多个动作集。iOS会在后台管理和运行你的触发器。[HMTrigger](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMTrigger_Class/index.html#//apple_ref/occ/cl/HMTrigger)类是[HMTimerTrigger](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMTimerTrigger_Class/index.html#//apple_ref/occ/cl/HMTimerTrigger)具体类的抽象类。当你创建一个定时触发器时,你需要指定触发时间和触发的周期。创建并开启一个定时触发器需要多个步骤来完成。 遵循下面几步来创建并启动一个定时触发器 创建一个定时触发器 1.创建定时触发器。 ~~~ self.trigger = [[HMTimerTrigger alloc] initWithName:name fireDate:fireDate timeZone:niL recurrence:nil recurrenceCalendar:nil]; ~~~ 触发时间必须设置在将来的某个时刻,第二个参数必须为0.如果你设置了一个周期,周期的最小值是5分钟,最大值是5周。关于如何使用[NSDateComponents](https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSDateComponents_Class/index.html#//apple_ref/occ/cl/NSDateComponents)和[NSCalendar](https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSCalendar_Class/index.html#//apple_ref/occ/cl/NSCalendar)来设置周期,请阅读[Date and Time Programming Guide](https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/DatesAndTimes/DatesAndTimes.html#//apple_ref/doc/uid/10000039i) 2\. 添加一个动作集到触发器。 使用[HMTrigger](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMTrigger_Class/index.html#//apple_ref/occ/cl/HMTrigger)基类方法[addActionSet:completionHandler:](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMTrigger_Class/index.html#//apple_ref/occ/instm/HMTrigger/addActionSet:completionHandler:),来添加一个动作集到触发器。 3\. 添加一个触发器到家庭。 使用HMHome类中的[addTrigger:completionHandler:](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMHome_Class/index.html#//apple_ref/occ/instm/HMHome/addTrigger:completionHandler:)方法来添加一个触发器到家庭。 4\. 启动触发器。 新创建的触发器默认是未启动的。需要使用[enable:complationHandler:](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMTrigger_Class/index.html#//apple_ref/occ/instm/HMTrigger/enable:completionHandler:)方法启动触发器。 一个定时触发器被启动后,会周期性的运行它的动作集。