Search code examples
swiftmedia-playerios13

App crashes only on iOS 13: "Must return MPRemoteCommandHandlerStatus or take a completion handler as the second argument."


My code works perfect on iOS 12 and lower:

fileprivate func setupRemoteControl() {

    UIApplication.shared.beginReceivingRemoteControlEvents()
    let commandCenter = MPRemoteCommandCenter.shared()
    commandCenter.playCommand.isEnabled = true
    commandCenter.playCommand.addTarget { (_) -> MPRemoteCommandHandlerStatus in
        self.player.play()
        self.playPause.setImage(#imageLiteral(resourceName: "PausePlayer"), for: .normal)
        self.miniPlayPauseButton.setImage(#imageLiteral(resourceName: "PausePlayerMini"), for: .normal)
        self.setupElapsedTime()
        MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPNowPlayingInfoPropertyPlaybackRate] = 1
        return .success
    }
    commandCenter.pauseCommand.isEnabled = true
    commandCenter.pauseCommand.addTarget { (_) -> MPRemoteCommandHandlerStatus in
        self.player.pause()
        self.playPause.setImage(#imageLiteral(resourceName: "PlayPlayer"), for: .normal)
        self.miniPlayPauseButton.setImage(#imageLiteral(resourceName: "PlayPlayerMini"), for: .normal)
        self.setupElapsedTime()
        MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPNowPlayingInfoPropertyPlaybackRate] = 0
        return .success
    }
    commandCenter.togglePlayPauseCommand.isEnabled = true
    commandCenter.togglePlayPauseCommand.addTarget { (_) -> MPRemoteCommandHandlerStatus in
        self.handlePlayPause()
        return .success
    }
    commandCenter.skipForwardCommand.addTarget(self, action: #selector(handleSkipForward))
    commandCenter.skipBackwardCommand.addTarget(self, action: #selector(handleSkipBackward))
}

@objc fileprivate func handleSkipForward() {
    print("Seek 15 sec forward")
    let fifteenSeconds = CMTimeMakeWithSeconds(15, Int32(NSEC_PER_SEC))
    let seekTime = CMTimeAdd(player.currentTime(), fifteenSeconds)
    let elapsedTime = CMTimeGetSeconds(seekTime)
    MPNowPlayingInfoCenter.default().nowPlayingInfo![MPNowPlayingInfoPropertyElapsedPlaybackTime] = elapsedTime
    player.seek(to: seekTime)
}

@objc fileprivate func handleSkipBackward() {
    print("Seek 15 sec backward")
    let fifteenSeconds = CMTimeMakeWithSeconds(-15, Int32(NSEC_PER_SEC))
    let seekTime = CMTimeAdd(player.currentTime(), fifteenSeconds)
    let elapsedTime = CMTimeGetSeconds(seekTime)
    MPNowPlayingInfoCenter.default().nowPlayingInfo![MPNowPlayingInfoPropertyElapsedPlaybackTime] = elapsedTime
    player.seek(to: seekTime)
}

But when I try to run on iOS 13 simulator, app crashes with error in AppDelegate file:

"Thread 1: Exception: "Unsupported action method signature. Must return MPRemoteCommandHandlerStatus or take a completion handler as the second argument."

What is the problem?


Solution

  • Your code is not following the instruction shown in the doc:

    addTarget(_:action:)

    action

    A selector identifying the method on the target to be called. The value must not be NULL. The method to be called must have the following signature:

    - (MPRemoteCommandHandlerStatus) handleCommand: (MPRemoteCommandEvent*) event;
    

    Try changing the signature of the handler methods:

        @objc fileprivate func handleSkipForward(_: MPRemoteCommandEvent) -> MPRemoteCommandHandlerStatus {
            //...
            return .success
        }
    
        @objc fileprivate func handleSkipBackward(_: MPRemoteCommandEvent) -> MPRemoteCommandHandlerStatus {
            //...
            return .success
        }