Swift Developer
Swift Developer

Reputation: 247

Method for HMServiceGroup in Homekit

I am trying to using HMServiceGroup method in my app to handle all accessories at a time. But not success. Please tell me simple method for handle accessories using HMServiceGroup method.

Upvotes: 1

Views: 146

Answers (1)

Maria
Maria

Reputation: 4611

If all the services are of the same type, you can use a loop like this for specific characteristic types or use a big switch statement

for service in serviceGroup.services {
    for characteristic in service.characteristics {
        if characteristic.characteristicType == HMCharacteristicTypePowerState {
          //turn on or off whatever you want to do 
        }
    }
}

or to access the accessory you can do

for service in serviceGroup.services {
    if let accessory = service.accessory {
         //do whatever you need here
    }
}

Upvotes: 1

Related Questions