Slide 1

Slide 1 text

Ezequiel França @ezefranca HomeKit 101

Slide 2

Slide 2 text

EZEQUIEL FRANÇA iOS Developer, Maker and open-source hacker. @ezefranca http://ezefranca.com Mechatronics @ SENAI Electronics @ IFSP System Analysis @ FIAP

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

https://www.youtube.com/watch?v=BoeRfMBVCGo

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

HomeKit was introduced by Apple on iOS 8

Slide 7

Slide 7 text

HomeKit Accessories

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

Problem 35 Devices != = 35 Apps

Slide 10

Slide 10 text

35 Devices = 35 Apps 35 Devices = 35 Apps 35 Devices = 35 Apps 35 Devices = 35 Apps

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

iOS 10

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

News

Slide 15

Slide 15 text

News

Slide 16

Slide 16 text

MFi

Slide 17

Slide 17 text

News on HomeKit

Slide 18

Slide 18 text

News on HomeKit

Slide 19

Slide 19 text

News on HomeKit

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

Fim :)… ?

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

https://developer.apple.com/app-store/review/ guidelines/#homekit

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

Deal with HomeKit Every time update your UI status in completion handler block and in delegate methods!

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

!// MARK: HomeKit Variables let homeManager = HMHomeManager() var homes = [HMHome]()

Slide 38

Slide 38 text

!// 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) }

Slide 39

Slide 39 text

!// 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() } }

Slide 40

Slide 40 text

HMHome

Slide 41

Slide 41 text

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) }

Slide 42

Slide 42 text

!// 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)") }

Slide 43

Slide 43 text

•Observing Home Configuration •Observing Service Configuration •Observing Action and Trigger Configuration •Observing Accessories https://developer.apple.com/reference/homekit/hmhomedelegate

Slide 44

Slide 44 text

HMAccessory

Slide 45

Slide 45 text

// 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 } } }

Slide 46

Slide 46 text

// MARK: - Accessory Browser Delegate func accessoryBrowser(_ browser: HMAccessoryBrowser, didFindNewAccessory accessory: HMAccessory) { print("\(#function)") updateList() } func accessoryBrowser(_ browser: HMAccessoryBrowser, didRemoveNewAccessory accessory: HMAccessory) { print("\(#function)") updateList() }

Slide 47

Slide 47 text

HMService

Slide 48

Slide 48 text

// 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)") }

Slide 49

Slide 49 text

HMCharacteristic

Slide 50

Slide 50 text

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:

Slide 51

Slide 51 text

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") } }

Slide 52

Slide 52 text

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:

Slide 53

Slide 53 text

HMZone

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

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 }

Slide 57

Slide 57 text

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 } }

Slide 58

Slide 58 text

// MARK: HMHomeDelegate Methods func home(_ home: HMHome, didAdd zone: HMZone) { print("\(#function)") }

Slide 59

Slide 59 text

No content

Slide 60

Slide 60 text

HomeKit was introduced by Apple on iOS 8

Slide 61

Slide 61 text

News on iOS 10

Slide 62

Slide 62 text

HMHomeManager manage HMHome have HMRooms (or/and HMZones) have HMAccessory have HHService have HMCharacteristic

Slide 63

Slide 63 text

https://www.apple.com/ios/home/accessories/

Slide 64

Slide 64 text

No content

Slide 65

Slide 65 text

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