title: how can I make the torch/flash-on reliable when coming from multitask
body: <p>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.</p>
  
  <p>AppDeligate.m</p>
  
  <pre><code>- (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] &amp;&amp; [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 &amp;&amp; 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 &amp;&amp; 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];
  }
  </code></pre>
  
  <p>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.</p>