Jolaroux Jolaroux - 1 year ago 129
iOS Question

Added accessories have no services available after adding services in HomeKit simulator?

So I'm trying to write a simple app to control my home kit devices, but I'm having trouble with the code.

Here is my code for browsing home kit accessories. But when I


I get 0. Here is some of my code:

import UIKit
import HomeKit

class DiscoveryTableViewController: UITableViewController, HMAccessoryBrowserDelegate {

@IBAction func backButton(_ sender: AnyObject) {
navigationController?.dismiss(animated: true, completion: nil)

@IBOutlet weak var saveButton: UIBarButtonItem!

let homeManager = HMHomeManager()
let browser = HMAccessoryBrowser()

var accessories = [HMAccessory]()
var selectedAcc: HMAccessory?

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)

//setting the title to "Searching..."
title = "Searching..."

//setting the browser delegate = to self
browser.delegate = self
//searching for accessories

//only searching for a short time to be efficient
Timer.scheduledTimer(timeInterval: 5.0, target: self, selector: #selector(DiscoveryTableViewController.stopSearching), userInfo: nil, repeats: false)


func stopSearching() {
title = "Discovered"

override func didReceiveMemoryWarning() {
// Dispose of any resources that can be recreated.

// MARK: - Table view data source

override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return accessories.count


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "DiscoveryTableViewCell"
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! DiscoveryTableViewCell
let accessory = accessories[(indexPath as NSIndexPath).row] as HMAccessory

// Configure the cell...
cell.discNameLabel.text =


return cell

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
selectedAcc = accessories[indexPath.row]
performSegue(withIdentifier: "showDeviceDetail", sender: self)
//performSegue(withIdentifier: "UnwindToDeviceTable", sender: self)

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showDeviceDetail" {
if let destination = segue.destination as? AddDeviceViewController {
destination.addedAcc = selectedAcc

// Informs us when we've located a new accessory in the home
func accessoryBrowser(_ browser: HMAccessoryBrowser, didFindNewAccessory accessory: HMAccessory) {


func accessoryBrowser(_ browser: HMAccessoryBrowser, didRemoveNewAccessory accessory: HMAccessory) {
var index = 0
for item in accessories {
if == {
accessories.remove(at: index)
break; // done
index += 1


In this class, I am browsing all my accessories. I can access all the names of all the devices, but when I print the "
", I get 0 every time. Can someone help? And yes I have added services and characteristics under each accessory in the

Answer Source

I just confirmed this, services are not available until after you add an accessory to a home, which implies that you have paired with the accessory:

homeManagerInstance.primaryHome?.addAccessory(accessory, completionHandler: { (error) in
   print("ACCESSORY SERVICES: \(")

If you try to check service.count before that, it does indeed come back as zero.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download