Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥

[TDC Florianopolis 2017] - HomeKit 101

[TDC Florianopolis 2017] - HomeKit 101

No lançamento do iOS 8 a Apple anunciou o Homekit, framework da empresa dedicado a conexão e comunicação com dispositivos inteligentes. Controle a temperatura, abra portas, acesse a camêras de segurança e muito mais. Nessa talk veremos a evolução do Homekit, sua integração com a assistente pessoal Siri, alguns exemplos de implementação e como começar a desenvolver com este framework.

Ezequiel Santos

May 06, 2017
Tweet

More Decks by Ezequiel Santos

Other Decks in Technology

Transcript

  1. EZEQUIEL FRANÇA iOS Developer, Maker and open-source hacker. @ezefranca http://ezefranca.com

    Mechatronics @ SENAI Electronics @ IFSP System Analysis @ FIAP
  2. 35 Devices = 35 Apps 35 Devices = 35 Apps

    35 Devices = 35 Apps 35 Devices = 35 Apps
  3. MFi

  4. Deal with HomeKit Every time update your UI status in

    completion handler block and in delegate methods!
  5. !// MARK: Add Home func addHomeWithName(_ name: String) { homeManager.addHome(withName:

    name) { newHome, error in if let error = error { return } self.reloadHomes() } } !// MARK: Remove Home func removeHomeAtIndexPath(_ indexPath: IndexPath) { let home = homes[indexPath.row] homeManager.removeHome(home) { error in if let error = error { return } self.homes.remove(at: indexPath.row) self.reloadHomes() } } !// MARK: Reload Homes func reloadHomes() { homes.removeAll() homes.append(contentsOf: homeManager.homes) }
  6. !// MARK: Setup Delegates func setupDelegates() { homeManager.delegate = self

    } !// MARK: HMHomeManagerDelegate Methods extension ViewController : HMHomeManagerDelegate { func homeManagerDidUpdateHomes(_ manager: HMHomeManager) { print("\(#function)") self.reloadHomes() } func homeManager(_ manager: HMHomeManager, didAdd home: HMHome) { print("\(#function)") self.reloadHomes() } func homeManager(_ manager: HMHomeManager, didRemove home: HMHome) { print("\(#function)") self.reloadHomes() } }
  7. var activeHome: HMHome! var rooms:[HMRoom]? !// MARK: Add Room func

    addRoomWithName(_ name: String) { activeHome.addRoom(withName: name) { newRoom, error in if let error = error { return } self.reloadRooms() } } !// MARK: Remove Room func removeRoomAtIndexPath(_ indexPath: IndexPath) { let room = rooms[indexPath.row] activeHome.removeRoom(room) { error in if let error = error { return } self.rooms.remove(at: indexPath.row) self.reloadRooms() } } !// MARK: Reload Rooms func reloadRooms() { rooms.removeAll() rooms.append(contentsOf: activeHome.rooms) }
  8. !// MARK: HMHomeDelegate Methods func home(_ home: HMHome, didAdd room:

    HMRoom) { print("\(#function)") } func home(_ home: HMHome, didAdd zone: HMZone) { print("\(#function)") } func home(_ home: HMHome, didAdd group: HMServiceGroup) { print("\(#function)") } func home(_ home: HMHome, didRemove user: HMUser) { print("\(#function)") }
  9. •Observing Home Configuration •Observing Service Configuration •Observing Action and Trigger

    Configuration •Observing Accessories https://developer.apple.com/reference/homekit/hmhomedelegate
  10. // MARK: HomeKit Variables var activeHome: HMHome! let accessoryBrowser =

    HMAccessoryBrowser() var accessories = [HMAccessory]() // MARK: Setup Delegates func setupDelegates() { accessoryBrowser.delegate = self accessoryBrowser.startSearchingForNewAccessories() //accessoryBrowser.stopSearchingForNewAccessories() } func addAccessory(_ accessory: HMAccessory) { activeHome.addAccessory(accessory) { error in if let error = error { return } } }
  11. // MARK: - Accessory Browser Delegate func accessoryBrowser(_ browser: HMAccessoryBrowser,

    didFindNewAccessory accessory: HMAccessory) { print("\(#function)") updateList() } func accessoryBrowser(_ browser: HMAccessoryBrowser, didRemoveNewAccessory accessory: HMAccessory) { print("\(#function)") updateList() }
  12. // MARK: HomeKit Variables var activeAccessory: HMAccessory! var activeService: HMService!

    // MARK: HMAccessoryDelegate Methods func accessoryDidUpdateReachability(_ accessory: HMAccessory) { print("\(#function)") if accessory !== activeService.accessory && ! accessory.isReachable { return } } func accessory(_ accessory: HMAccessory, service: HMService, didUpdateValueFor characteristic: HMCharacteristic) { print("\(#function)") }
  13. func predeterminedValueDescriptionForNumber(_ number: Int) -> String? { switch self.characteristicType {

    case HMCharacteristicTypePowerState, HMCharacteristicTypeInputEvent, HMCharacteristicTypeOutputState: if Bool(number as NSNumber) { return NSLocalizedString("On", comment: "On") } else { return NSLocalizedString("Off", comment: "Off") } case HMCharacteristicTypeOutletInUse, HMCharacteristicTypeMotionDetected, HMCharacteristicTypeAdminOnlyAccess, HMCharacteristicTypeAudioFeedback, HMCharacteristicTypeObstructionDetected:
  14. case HMCharacteristicTypeTargetHeatingCooling: if let mode = HMCharacteristicValueHeatingCooling(rawValue: number) { switch

    mode { case .off: return NSLocalizedString("Off", comment: "Off") case .heat: return NSLocalizedString("Heat", comment: "Heat") case .cool: return NSLocalizedString("Cool", comment: "Cool") case .auto: return NSLocalizedString("Auto", comment: "Auto") } }
  15. case HMCharacteristicTypeTargetDoorState, HMCharacteristicTypeCurrentDoorState: if let doorState = HMCharacteristicValueDoorState(rawValue: number) {

    switch doorState { case .open: return NSLocalizedString("Open", comment: "Open") case .opening: return NSLocalizedString("Opening", comment: "Opening") case .closed: return NSLocalizedString("Closed", comment: "Closed") case .closing: return NSLocalizedString("Closing", comment: "Closing") case .stopped: return NSLocalizedString("Stopped", comment:
  16. open class HMZone : NSObject { /*! * @brief Name

    of the zone. */ open var name: String { get } /*! * @brief Array of HMRoom objects that correspond to the rooms contained in this zone. */ open var rooms: [HMRoom] { get }
  17. let room:HMRoom! var activeZone: HMZone! activeZone.removeRoom(room) { error in if

    let error = error { return } } activeZone.addRoom(room) { error in if let error = error { return } }