I am updating old media player project (Swift 3) and have weird error:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Unsupported action method signature. Must return MPRemoteCommandHandlerStatus or take a completion handler as the second argument.
I get this error on this code:
UIApplication.shared.beginReceivingRemoteControlEvents()
let commandCenter = MPRemoteCommandCenter.shared()
commandCenter.previousTrackCommand.addTarget(self, action: #selector(handlePrevTrack))
@objc func handlePrevTrack(){
if playListEpisodes.count == 0 {
return
}
let currentEpisodeIndex = playListEpisodes.firstIndex { (ep) -> Bool in
return self.episode.title == ep.title && self.episode.author == ep.author
}
guard let index = currentEpisodeIndex else { return}
let nextEpisode:Episode
if index == 0 {
nextEpisode = playListEpisodes[playListEpisodes.count - 1]
}else {
nextEpisode = playListEpisodes[index - 1]
}
self.episode = nextEpisode
}
what changed in Swift 5.1?
According to documentation, previousTrackCommand
selector should take MPRemoteCommandEvent
as first argument and return MPRemoteCommandHandlerStatus
:
func addTarget(handler: (MPRemoteCommandEvent) -> MPRemoteCommandHandlerStatus) -> Any
So your handlePrevTrack
function should be declared as:
@objc func handlePrevTrack(_ event: MPRemoteCommandEvent) -> MPRemoteCommandHandlerStatus {
...
}