I would like to present a UIImagePickerController immediately when a user tabs on the camera tab in my app to take a photo (similar to many photo taking apps like Instagram, etc). Currently, I have a VC called CameraViewController that displays the picture taken from the UIImagePickerController, and in the viewWillAppear function of CameraViewController I present the ImagePickerController modally when I detect that no picture have been taken.
While this worked most of the time, I noticed that at times I would get an exception saying:
"Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present modally an active controller ."
Per the suggestion of some SO questions here, I also tried presenting the controller in viewDidAppear. However, with viewDidAppear, the modal ImagePicker is never dismissed, and I keep getting the error "Unbalanced calls to begin/end appearance transitions for ."
My questions are:
- I am currently presenting the ImagePickerController modally for the user to take a picture, and then dismissing it once the picture is taken. Is this generally the way things are done with ImagePickerControllers that are the most reliable/problem-free.
- Where else should I consider putting the ImagePickerController in the view life cycle to get this desired effect?
Here is my code:
in viewWillAppear
BOOL modalPresent = (BOOL)(self.presentedViewController);
if (!appDelegate.imageStorageDictionary[@"picture1"]){
if (modalPresent == NO){
[self presentViewController:self.imagePickerController animated:NO completion:nil];
}
}
in imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
//Other lines omitted
if (![[self imagePickerController] isBeingDismissed])
[self dismissViewControllerAnimated:NO completion:nil];