Search code examples
iphoneobjective-ciosios5

Using torch and camera together on iOS - Doesn't work after background


I'm trying to open the camera and show it on the screen (inside my view) and also keep the torch on.

The problem I'm having is that it works on the first time I run the app but when the app goes to background and then foreground the camera freeze and show the last view.

Tested I've done:

  1. The torch seperatly works.
  2. Camera without torch works when foreground.

I saw that some other apps does the same have same problem.

Does anyone know or can think of a solution? (It's good enough if it would work on iOS5+)

Thanks.


Solution

  • In your view controller that controls the camera and torch, add an observer to the UIApplicationDidBecomeActiveNotification.

    -(void)viewDidLoad {
        [super viewDidLoad];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(becameActive) name:UIApplicationDidBecomeActiveNotification object:nil];
    }
    
    -(void)becameActive {
        //this code runs when you resume a suspended application
        //turn the camera torch on here
        //assuming you already have this code
    }
    
    -(void)viewDidUnload {
        [[NSNotificationCenter defaultCenter] removeObserver:self];
        [super viewDidUnload];
    }