[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.

42c9f7529d06c1e8f5da2c46ed4688d0?s=128

Ezequiel França

May 06, 2017
Tweet

Transcript

  1. Ezequiel França @ezefranca HomeKit 101

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

    Mechatronics @ SENAI Electronics @ IFSP System Analysis @ FIAP
  3. None
  4. https://www.youtube.com/watch?v=BoeRfMBVCGo

  5. None
  6. HomeKit was introduced by Apple on iOS 8

  7. HomeKit Accessories

  8. None
  9. Problem 35 Devices != = 35 Apps

  10. 35 Devices = 35 Apps 35 Devices = 35 Apps

    35 Devices = 35 Apps 35 Devices = 35 Apps
  11. None
  12. iOS 10

  13. None
  14. News

  15. News

  16. MFi

  17. News on HomeKit

  18. News on HomeKit

  19. News on HomeKit

  20. None
  21. None
  22. None
  23. Fim :)… ?

  24. None
  25. None
  26. None
  27. None
  28. https://developer.apple.com/app-store/review/ guidelines/#homekit

  29. None
  30. None
  31. Deal with HomeKit Every time update your UI status in

    completion handler block and in delegate methods!
  32. None
  33. None
  34. None
  35. None
  36. None
  37. !// MARK: HomeKit Variables let homeManager = HMHomeManager() var homes

    = [HMHome]()
  38. !// 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) }
  39. !// 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() } }
  40. HMHome

  41. 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) }
  42. !// 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)") }
  43. •Observing Home Configuration •Observing Service Configuration •Observing Action and Trigger

    Configuration •Observing Accessories https://developer.apple.com/reference/homekit/hmhomedelegate
  44. HMAccessory

  45. // 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 } } }
  46. // MARK: - Accessory Browser Delegate func accessoryBrowser(_ browser: HMAccessoryBrowser,

    didFindNewAccessory accessory: HMAccessory) { print("\(#function)") updateList() } func accessoryBrowser(_ browser: HMAccessoryBrowser, didRemoveNewAccessory accessory: HMAccessory) { print("\(#function)") updateList() }
  47. HMService

  48. // 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)") }
  49. HMCharacteristic

  50. 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:
  51. 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") } }
  52. 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:
  53. HMZone

  54. None
  55. None
  56. 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 }
  57. 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 } }
  58. // MARK: HMHomeDelegate Methods func home(_ home: HMHome, didAdd zone:

    HMZone) { print("\(#function)") }
  59. None
  60. HomeKit was introduced by Apple on iOS 8

  61. News on iOS 10

  62. HMHomeManager manage HMHome have HMRooms (or/and HMZones) have HMAccessory have

    HHService have HMCharacteristic
  63. https://www.apple.com/ios/home/accessories/

  64. None
  65. That’s it http://speakerdeck.com/ezefranca http://github.com/ezefranca/homekit101-tdc Ezequiel França @ezefranca | www.ezefranca.com