Creating a HomeKit-enabled App with SwiftUI

Understand how to create a HomeKit-enabled App with SwiftUI, how to read data from smart home accessories, and control their features.

Creating a HomeKit-enabled App with SwiftUI

With the official launch of Apple-supported matter in October 2022, home automatisation will become more approachable with less fragmentation among vendors. It is supported by most relevant players, such as Amazon, Google, Comcast, IKEA and of course Apple, so it is time to learn more about developing HomeKit-enabled apps.

This tutorial will cover the very basics of finding accessories added to your Home within the Home app, how to identify available features and then how to read and write data to change the settings of your smart accessories. It will not cover the implementation of Matter, which will be supported from iOS 16.1 onwards. If you want to dive into the Matter Framework, explore the Apple Developer Documentation or wait for the next article in our series.

If you are new to HomeKit, this is how to get started.

Creating a new App with HomeKit Capabilities

In Xcode, let's create a new app project using SwiftUI. I named my app MyHomeKitApp. The app will be quite simple and just use List Views to show what is available in HomeKit. Once features are implemented, you might want to explore how to adopt more advanced architecture patterns, such as MVVM, or evolve the simplified UI to create a better user experience.

Fire, you have to add the HomeKit capability to the project. This can be done in the  Project Settings and the Signing & Capabilities pane of your target. When clicking on the "+" button in the preference pane, you can search the available capabilities for HomeKit to allow you to interact with HomeKit accessories.

Once added, Xcode will prompt you to download Apple's HomeKit Accessory Simulator (HAS). The HAS comes as a part of the Additional Tools for Xcode package and can be downloaded from the Apple Developer Website. It comes in handy in case you don't have many smart home accessories and want to simulate setups without having to buy any hardware.

A more detailed explanation of how the HomeKit Accessory Simulator can be found in our article "Developing Apps with the HomeKit Accessory Simulator" or in the Apple Developer Documentation.

As your app will access HomeKit and as part of that potentially sensitive information about your smart home setup, e.g. security cameras, automatisation schemes etc., you need an entitlement to your projects info.plist to ask the user for permission to access HomeKit.

In your project settings Info pane you can add a new entry for the Custom iOS Target Properties. Provide a meaningful description of why you need access to HomeKit for the NSHomeKitUsageDescription key.  

As a consequence iOS will automatically generate an Alert for the user when the app tries to access HomeKit for the first time. The alert will include your app name and the usage description provided in the plist entry. Also notice, that users can change these permissions at any time through the Settings app. Therefore it might be smart to incorporate frequent checks for permissions in your app.

Without the usage description, your app will crash when you first try to use HomeKit, so make sure you don't forget it. 

HomeKit Essentials

Before we get going, here is some more information about the information architecture inside HomeKit. This is critical to understand and while confusing labeling may exist, it is quite straightforward.

HomeKit organises the control of smart home accessories in Homes, of which you can create as many as you like. Within Homes there are Rooms that can be grouped in Zones, which are basically collections of Rooms.

Inside each Room, there can then be any number of Accessories which in turn will provide a number of Services that provide Characteristics. For example, a smart lightbulb may come with Services such as a light sensor and an actual control for the light bulb itself. The light sensor then may have Characteristics for detecting the ambient light and indicating whether the sensor is operative, while the lightbulb may come with Characteristics such as power state and brightness level etc.

These Characteristics can then be used to control the smart accessories, for example turning lights on or off, and changing brightness or color. Also, they can be used for automatisation based on specific values, for example turning on the light when the ambient light goes below a certain threshold.

HomeKit: Homes and Roome with Accessories, their Services, and Characteristics

Based on this, let's build an app that will display any number of Homes available in HomeKit, any Accessories, their Services, and their Characteristics. Then, let's also understand how we can read values from and write new values to the Characteristics to control the accessory.

Become a free member or log in to proceed.