torchlevel not setting properly on iphone xs max

290 Views Asked by At

I made this app dozing that helps you meditate by adjusting the torch on your phone. It goes from a low-level brightness to a high-level birghtness over a certain time period.

It worked great for iPhone 6gen, 7gen, 8gen, and X. I just got the XS max, and for whatever reason instead of the brightness adjusting, it just stays turned on at maximum brightness.

It still works on the iPhone 7 with iOS 12. Weird thing is, SOMETIMES it RANDOMLY works on the XS Max, I just can't figure out what causes it to adjust properly and what causes it to sometimes be stuck on maximum brightness.

func updateTorch() {

    guard let device = AVCaptureDevice.default(for: AVMediaType.video) 
else {
        return
    }

    if device.hasTorch && device.isTorchAvailable {

        do {
            try device.lockForConfiguration()

            if torchMode == 0 {
                device.torchMode = .off
            } else {
                try device.setTorchModeOn(level: torchMode) // HERE
            }

            device.unlockForConfiguration()

        } catch {
            print("Torch is not working.")
        }
    } else {
        print("Torch not compatible with device.")
    }
}

That's my main method that updates the torch. If I print "torchMode" where I marked "//HERE," it gives an ajusting Float value between 0 and 1. Also no errors are thrown from the setTorchModeOn(level:) method.

0

There are 0 best solutions below