Poorna
Poorna

Reputation: 143

How to execute a method when user quit iOS application (Swift)?

I've been building iOS Music application and I'm using MPMusicPlayerController.systemMusicPlayer() to play music from the iTunes library of the device.

I tried applicationWillTerminate to stop the music player, and It didn't work, after doing some research that I found out After iOS 4 applicationWillTerminate method will *almost never get called, and we must do all the work in applicationDidEnterBackground method.

But my point is, I want my Music player to keep playing music when it enters to the background, and only stop the music when user force quit the application, How can I achieve this behavior ?

Upvotes: 0

Views: 2941

Answers (1)

Mitesh jadav
Mitesh jadav

Reputation: 930

Important: The applicationWillTerminate: method is not called if your app is currently suspended.

Even if you develop your app using iOS SDK 4 and later, you must still be prepared for your app to be killed without any notification. The user can kill apps explicitly using the multitasking UI. In addition, if memory becomes constrained, the system might remove apps from memory to make more room. Suspended apps are not notified of termination but if your app is currently running in the background state (and not suspended), the system calls the applicationWillTerminate: method of your app delegate. Your app cannot request additional background execution time from this method.

https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/TheAppLifeCycle/TheAppLifeCycle.html

Upvotes: 1

Related Questions