This is my working code in Swift. The issue is that I'm using UInt
as an intermediary type.
func handleInterruption(notification: NSNotification) {
let interruptionType = notification.userInfo?[AVAudioSessionInterruptionTypeKey] as! UInt
if (interruptionType == AVAudioSessionInterruptionType.Began.rawValue) {
// started
} else if (interruptionType == AVAudioSessionInterruptionType.Ended.rawValue) {
// ended
let interruptionOption = notification.userInfo?[AVAudioSessionInterruptionOptionKey] as! UInt
if interruptionOption == AVAudioSessionInterruptionOptions.OptionShouldResume.rawValue {
// resume!
}
}
}
Is there a better way?
This approach is similar to Matt's, but due to changes with Swift 3 (mainly
userInfo
being[AnyHashable : Any]
), we can make our code a little more "Swifty" (no switching onrawValue
or casting toAnyObject
, etc.):