应用仅在 iOS 13 崩溃:"Must return MPRemoteCommandHandlerStatus or take a completion handler as the second argument."

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

我的代码在 iOS 12 及更低版本上运行完美:

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)
}

但是当我尝试在 iOS 13 模拟器上 运行 时,应用程序崩溃并在 AppDelegate 文件中出现错误:

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

有什么问题?

您的代码未遵循文档中显示的说明:

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;

尝试更改处理程序方法的签名:

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

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