<!--
{
  "availability" : [
    "macOS: 15.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Virtualization",
  "identifier" : "/documentation/Virtualization/VZUSBController/attach(device:completionHandler:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Virtualization"
    ],
    "preciseIdentifier" : "c:objc(cs)VZUSBController(im)attachDevice:completionHandler:"
  },
  "title" : "attach(device:completionHandler:)"
}
-->

# attach(device:completionHandler:)

Attaches a USB device to the controller.

```
func attach(device: any VZUSBDevice, completionHandler: @escaping ((any Error)?) -> Void)
```

## Parameters

`device`

The USB device to attach.

`completionHandler`

A block the framework calls after the device attaches, or on an error. The error parameter that passes to the block is `nil` if attaching is successful. The framework calls the block on a VM’s queue.

## Discussion

If the device successfully attaches to the controller, it appears in the [`usbDevices`](/documentation/Virtualization/VZUSBController/usbDevices) property, the framework sets its [`usbController`](/documentation/Virtualization/VZUSBDevice/usbController) property to point to the attached USB controller, and the completion handler returns `nil`.

If the device has a previous attachment to the current USB controller, or to another USB controller, the attach function fails with [`VZError.Code.deviceAlreadyAttached`](/documentation/Virtualization/VZError/Code/deviceAlreadyAttached). If the controller can’t initialize the device correctly, the attach function fails with [`VZError.Code.deviceInitializationFailure`](/documentation/Virtualization/VZError/Code/deviceInitializationFailure).

You need to call this method on the virtual machine’s queue.

## See Also

[`VZUSBDevice`](/documentation/Virtualization/VZUSBDevice)

A protocol that represents a USB device in a VM.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)