I faced a similar situation, and from my own experience I found that viewWillDisappear: does not receive the call. I really don't know why, but I solved this by signing up for notifications when the application is inactive. Here is an example:
In the WillAppear view:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appInactive:) name:UIApplicationWillResignActiveNotification object:[UIApplication sharedApplication]];
And the corresponding callback method:
- (void)appInactive:(NSNotification *)notification {
NSLog(@"App going inactive, stopping recording...");
taskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler: ^{
[[UIApplication sharedApplication] endBackgroundTask:taskId];
taskId = UIBackgroundTaskInvalid;
}];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
question.prepTimeRemaining = [prepEndTime timeIntervalSinceNow];
if (recording)
[self stopRecording];
[[UIApplication sharedApplication] endBackgroundTask:taskId];
taskId = UIBackgroundTaskInvalid;
});
}
In the WillDisappear view:
[[NSNotificationCenter defaultCenter] removeObserver:self];
I immediately go on to the next view when I discover this, so I'm not sure if it leaves at the preview level, but I suspect that it will do what you want. Hope this helps!
source
share