应用程序在后台时,iPhone手电筒不起作用 [英] Iphone flashlight not working while app is in background

查看:338
本文介绍了应用程序在后台时,iPhone手电筒不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在使用基于位置的应用程序,想在后台使用iphone相机闪光灯.不幸的是,闪光灯仅在前台工作,即使代码正在执行,它也会在后台自动关闭闪光灯.

我使用的代码仅在前台工作

The code i used is working only in 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];
    }
}

推荐答案

这是正常现象.
Apple的沙箱操作不允许您在应用程序在后台运行时保持闪光灯打开.
除非我们谈论的是越狱应用程序,否则没有解决方法.

This is a normal behavior.
Apple's sandboxing will not allow you to keep the flash on while your app is in the background.
There is no workaround unless we are talking about a jailbroken app.

修改:
苹果对系统API的使用非常严格.特别是在以下方面:
-用户隐私
-电池寿命
-用户体验


Apple is very strict on it's system's APIs usage. Especially when it comes to:
- User privacy
- Battery life
- User experience

对于手电筒,最后两项是相关的. Apple不会在不在前台的情况下让应用耗尽电池电量,并且iOS用户习惯了进入背景时会立即禁用手电筒,相机,麦克风...的事实(某些背景模式下的麦克风除外)情况).

In the case of the flashlight, the last two items are relevant. Apple will not let an app drain the battery when not in the foreground and iOS users are used to the fact that flashlight, camera, microphone... are immediately disabled when going to the background (with an exception for the microphone in some background modes cases).

要回答您的第一篇文章中的评论,iOS将控制您的硬件.因此,由于Apple决定当用户关闭应用程序时不希望灯一直亮着,因此iOS只会在您的应用程序进入后台时关闭闪光灯电源.您的应用无权阻止它.

To answer the comment on your initial post, iOS controls your hardware. So since Apple decided they don't want the light to stay on when the user closes the app, iOS will just power off the flash when your app goes in the background. Your app has no authority to prevent it.

这篇关于应用程序在后台时,iPhone手电筒不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆