You can listen to the UIApplicationDidEnterBackgroundNotification notification using the NSNotificationCenter.
With that, you can setup a method to be called whenever the app goes into the background where you can check if the picker is being displayed (perhaps store a reference to it whenever it is being displayed):
- (void)presentImagePicker
{
if (self.displayedPicker) {
return;
}
self.displayedPicker = [[UIImagePickerController alloc] init];
self.displayedPicker.delegate = self;
self.displayedPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:self.displayedPicker animated:YES completion:nil];
// Register for the method to be called every time the app enters the background
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(didEnterBackgroundNotification:)
name:UIApplicationDidEnterBackgroundNotification
object:nil
];
}
- (void)dismissImagePickerAnimated:(BOOL)animated
{
if (self.displayedPicker) {
[self.displayedPicker dismissViewControllerAnimated:animated];
self.displayedPicker = nil;
// Unregister from notification
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:UIApplicationDidEnterBackgroundNotification
object:nil
];
}
}
- (void)didEnterBackgroundNotification:(NSNotification *)notification
{
[self dismissImagePickerAnimated:NO];
}