Iphone flashlight does not work when the application is in the background

Hi, I am using a location based application and want to use a flashlight for iphone camera in background. Unfortunately, the flashlight only works in the foreground, it automatically turns off the flash in the background, even if the code is executed.

The code I use only works in the foreground

#import <AVFoundation/AVFoundation.h> //flashcode Class captureDeviceClass = NSClassFromString(@"AVCaptureDevice"); if (captureDeviceClass != nil) { AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; if ([device hasTorch] && [device hasFlash]){ [device lockForConfiguration:nil]; if (device.torchMode == AVCaptureTorchModeOff) { [device setTorchMode:AVCaptureTorchModeOn]; [device setFlashMode:AVCaptureFlashModeOn]; //torchIsOn = YES; } else { [device setTorchMode:AVCaptureTorchModeOff]; [device setFlashMode:AVCaptureFlashModeOff]; // torchIsOn = NO; } [device unlockForConfiguration]; } } 
+8
objective-c background ios8 camera flashlight
source share
1 answer

This is normal behavior.
Apple sandboxing will not let you turn on the flash while your app is in the background.
There is no workaround if we are not talking about a jailbroken application.

Edit:
Apple is very tough on using system APIs. Especially when it comes to:
- User privacy
- Battery Life
- User experience

In the case of a flashlight, the last two elements are relevant. Apple will not allow the device to drain the battery, if not in the foreground, and iOS users are used to make the flashlight, camera, microphone ... immediately turn off when switching to the background (excluding the microphone in some background cases).

To respond to a comment on your original post, iOS controls your hardware. Since Apple decided that they do not want the light to remain on when the user closes the application, iOS will simply turn off the flash when your application leaves the background. Your application is not authorized to prevent it.

+11
source share

All Articles