devices(for:) was deprecated in macOS 10.15: Use AVCaptureDeviceDiscoverySession instead

In my app, I had a call to AVCaptureDevice.devices(), which looked like:

let avCaptureDevices = AVCaptureDevice.devices(for: AVMediaType.video)

Since macOS 10.15, this generates this deprecation warning:

devices(for:) was deprecated in macOS 10.15: Use AVCaptureDeviceDiscoverySession instead.

Xcode sometimes gives a nice “Fix” button for deprecations, but not this time. So what is the equivalent of my old call to AVCaptureDevice.devices()? The Apple docs don’t say, but I believe it is as follows:

let deviceDiscoverySession = AVCaptureDevice.DiscoverySession(
    deviceTypes: [ .builtInWideAngleCamera, .externalUnknown ],
    mediaType: .video,
    position: .unspecified
)
let avCaptureDevices = deviceDiscoverySession.devices

Non-obviously, you should include the device type .externalUnknown. If you don’t include .externalUnknown, your users won’t be able to use that fancy new camera they bought. This device type includes USB webcams, which were only implicitly included in AVCaptureDevice.devices(for: AVMediaType.video).

Tagged #programming, #macos, #swift.

Similar posts

More by Jim

Want to build a fantastic product using LLMs? I work at Granola where we're building the future IDE for knowledge work. Come and work with us! Read more or get in touch!

This page copyright James Fisher 2020. Content is not associated with my employer. Found an error? Edit this page.