Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7913127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:46:16+00:00 2026-06-03T13:46:16+00:00

When I leave the app and come back to it’s ‘on’ sate sometimes it

  • 0

When I leave the app and come back to it’s ‘on’ sate sometimes it works fine by turning the torch/flash on but most of the time it either flashes or remains off.

AppDeligate.m

- (id) init {
torchState = TRUE;
if( (self=[super init] )) {
    /// initialize flashlight
    // test if this class even exists to ensure flashlight is turned on ONLY for iOS 4 and above
    Class captureDeviceClass = NSClassFromString(@"AVCaptureDevice");
    if (captureDeviceClass != nil) {

        AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

        if ([device hasTorch] && [device hasFlash]){

            if (device.torchMode == AVCaptureTorchModeOff) {

                NSLog(@"Setting up flashlight for later use...");

                AVCaptureDeviceInput *flashInput = [AVCaptureDeviceInput deviceInputWithDevice:device error: nil];
                AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init];

                AVCaptureSession *session = [[AVCaptureSession alloc] init];

                [session beginConfiguration];
                [device lockForConfiguration:nil];

                [session addInput:flashInput];
                [session addOutput:output];

                [device unlockForConfiguration];

                [output release];

                [session commitConfiguration];
                [session startRunning];

                [self setTorchSession:session];
                [session release];
            }

        }

    } 
} 
return self;
}

- (void)toggleTorch {
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    [device lockForConfiguration:nil];
    // For the first 4 to 5 times comming back from multiask this first if hits and works properly

    if (torchState == TRUE && device.torchMode == AVCaptureTorchModeOff) {
        NSLog(@"AVCaptureTorchModeOff setting On");
        // On the 4th or 5th time it flashes and stays off or does nothing staying OFF
        // even though the NSLog fires

        [device setTorchMode:AVCaptureTorchModeOn];
        [device setFlashMode:AVCaptureFlashModeOn];

    } else if (torchState == TRUE && device.torchMode == AVCaptureTorchModeOn) {
        // Sometimes this randomly fires and every time ErrorNotification fires too
        NSLog(@"AVCaptureTorchModeOn");
        if (AVCaptureSessionRuntimeErrorNotification) {
            NSLog(@"ERROR"); 
            // Try to force but doesn't do anything
            [device setTorchMode:AVCaptureTorchModeOn];
            [device setFlashMode:AVCaptureFlashModeOn];
        }    
    } else {
        NSLog(@"ALL IS OFF");
        // when torch is in the off state it returns off as it should
        torchState = FALSE;
        [device setTorchMode:AVCaptureTorchModeOff];
        [device setFlashMode:AVCaptureFlashModeOff];
    }
    [device unlockForConfiguration];
}

-(void) applicationDidEnterForeground:(UIApplication*)application {
    [self toggleTorch];
}

The only thing I haven’t included in code is a touch even that calls toggleTorch for on/off. That piece works great so again, what I’m testing here is turning it on at launch aka DidEnterForeground as well as when the app is returned to from a multitask session.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T13:46:16+00:00Added an answer on June 3, 2026 at 1:46 pm

    I would change 2 things:
    1. add it to ApplicationDidBecomeActive
    2. don’t use toggle torch but set the state you want it to be (so you won’t turn it off twice or on twice…). So change the function to toggleTorchWithState…

    That what I’ve done in one of my apps and that works perfectly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a asp.net/c# web app. When do user leave a certain page, I
I need to proxy a feed and leave most of the nodes unspoiled and
I want to control how easily people can leave my app. Now, I know
So, I'm streaming music in a separate thread. If I leave the app (
I want the user to start the app and leave it running. At one
If application is gone to background and come back after 5 minutes, I need
If I come across old code that does if (!this) return; in an app,
I've noticed that when I leave my app searching for an automatch, it stops
everyone. 1) I've always thought that if I leave my app through Home button
I have a one page web app so there is no page refreshing. Sometimes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.