Reputation: 35
I made an app that is a player. When the user dismiss the app (enter in background mode) and call viewWillDisappear the player stops the audio.
I can't stop in applicationDidEnterBackground because in this method I play the audio already for make the audio accessible in background.
But the problem occurs when inside the app I want to change the view and call viewWillDisappear and obviously the audio stop.
Are any option to call viewWillDisappear only when the user dismiss the app or not call viewWillDisappear when the user change the selected tabbar or open other viewcontroller.
Thanks
Upvotes: 1
Views: 2197
Reputation: 16318
viewWillDisappear
is a callback method and is part of view lifecycle and you have no control over when it gets called.
You can refer to apple docs (link):
This method is called in response to a view being removed from a view hierarchy. This method is called before the view is actually removed and before any animations are configured.
Subclasses can override this method and use it to commit editing changes, resign the first responder status of the view, or perform other relevant tasks. For example, you might use this method to revert changes to the orientation or style of the status bar that were made in the viewDidDisappear: method when the view was first presented. If you override this method, you must call super at some point in your implementation.
Upvotes: 3
Reputation: 58097
Like @Bhumit said, you can't control the callback methods of the view lifecycle (or the application lifecycle.) They're called at specific times while the user interacts with your application.
Let's consider what you might want to do here:
- (void)viewWillDisappear
{
[super viewWillDisappear];
[[AudioPlayerClass sharedInstance] stop]; // an example way to stop playing the audio
}
viewWillDisappear
gets called whenever a user dismisses a modal view, navigates to new view on a navigation controller, or taps on a tab in a tab bar controller. If your user opens a settings tab, the audio will stop. If they open another tab, it'll stop. You've described this problem already.
We want the app to stop playing audio when the view disappears and some other condition is fulfilled. Let's look at that in code:
- (void)viewWillDisappear
{
[super viewWillDisappear];
if (something)
{
[[AudioPlayerClass sharedInstance] stop]; // an example way to stop playing the audio
}
}
That condition, according to your question, is when the app is closed.
What if there were some way to only change the audio if the view disappears and the app is closed?
The truth is that we can simplify this because if the app is closing, then the view is also disappearing. So what we really want is:
if(/* app is closing */)
{
/* Stop the music. */
}
Well, for that we have app lifecycle methods defined in the app delegate. One that you might want to use is applicationWillResignActive:
, which is called right before the app closes, but while it's still open.
- (void)applicationWillResignActive:(UIApplication *)application
{
[[AudioPlayerClass sharedInstance] stop]; // an example way to stop playing the audio
}
What confuses me is that you say that you use applicationDidEnterBackground
to make the audio accessible in the background, but then you want to stop playback while entering the background.
This confuses me because I'm not sure if you want to stop the audio or enable it. Also, you probably shouldn't be configuring playback sessions in a delegate method that runs right before the app deactivates.
You're much better off configuring the session once in application:didFinishLaunchingWithOptions:
or even better, applicationDidBecomeActive:
. This way, your app will have the correct session it needs, when it needs it, and then you'll be free to enter the states you want later on in the app lifecycle.
Upvotes: 3