I have an application that plays audio in the background. I am trying to fix a bug where the audio controls (play/pause), on the home screen (etc.), DO NOT work on iOS 8.0+
In your ViewController add
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
UIApplication.sharedApplication().beginReceivingRemoteControlEvents()
self.becomeFirstResponder()
}
override func remoteControlReceivedWithEvent(event: UIEvent) {
// your stuff
}
In AppDelegate add
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
var error: NSError?
AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: &error)
AVAudioSession.sharedInstance().setActive(true, error: &error)
}
SWIFT 3
UIApplication.shared.beginReceivingRemoteControlEvents()
self.becomeFirstResponder()
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)
} catch {
print("hmmm...")
}