Search code examples
iosiphoneswiftavaudioplayer

Alert "Sign in to iTunes Store" and "Battery low" pauses my apps jams. How can I unpause them?


I have some music in my app that plays under AVAudioSessionCategoryAmbient. Right now my problem is that when a popup from Apple like "Sign in to iTunes Store" pops up it pauses my music, but does not resume it. Thankfully it takes care of my SKScene... but not the music. Is there some way to have a callback for when the alert is dismissed?

The weird thing is that other UIAlertViews that I have made don't do this to the app, so I really don't know what is up.

List of alerts that pause scene and music:

  • Login to itunes
  • Battery low
  • Confirm purchase

Solution

  • I had similar problems sometime ago (beginning of Swift 1.2). I solved my problem by observing paused status for SKScene. SKScene have view which is SKView type.

    private var queueContext = 0
    
    override func didMoveToView(view: SKView) {
        super.didMoveToView(view)
        self.view.addObserver(self,
                              forKeyPath: "paused",
                              options: .New,
                              context: &queueContext)
    }
    
    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
        if context == &queueContext {
            if let paused = change?[NSKeyValueChangeNewKey] as? Bool {
                // TODO: react on paused status changes
            }
        }
        else {
            super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context)
        }
    }