Skip to content

Commit

Permalink
fix: Fix invalid userPreferredCameraDevice (#3068)
Browse files Browse the repository at this point in the history
* fix: Fix invalid `userPreferredCameraDevice`

* fix: Use `.default` API instead
  • Loading branch information
mrousavy authored Jul 10, 2024
1 parent 4d70469 commit 36fdf7c
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions package/ios/React/CameraDevicesManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,28 @@ final class CameraDevicesManager: RCTEventEmitter {
}

private func getPreferredDevice() -> AVCaptureDevice? {
#if swift(>=5.9)
if #available(iOS 17.0, *) {
if let userPreferred = AVCaptureDevice.userPreferredCamera {
// Return the device that was explicitly marked as a preferred camera by the user
return userPreferred
if #available(iOS 17.0, *) {
if let userPreferred = AVCaptureDevice.userPreferredCamera {
guard !userPreferred.uniqueID.isEmpty else {
// Due to an iOS 17 bug, Simulators return a non-nil AVCaptureDevice here,
// but all properties on this AVCaptureDevice are 0x0/empty/invalid.
// So we catch this bug and return a nil preferred device here.
return nil
}
// Return the device that was explicitly marked as a preferred camera by the user
return userPreferred
}
#endif
// Just return the first device
return discoverySession.devices.first
}
// Usually prefer the wide angle back camera
if let defaultBackWideAngle = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .back) {
return defaultBackWideAngle
}
// Fall back to the default video
if let defaultVideo = AVCaptureDevice.default(for: .video) {
return defaultVideo
}
// Return nil because apparently we couldn't find a default
return nil
}

private static func getAllDeviceTypes() -> [AVCaptureDevice.DeviceType] {
Expand Down

0 comments on commit 36fdf7c

Please sign in to comment.