No control interface appears on iOS app using MPRemoteCommandCenter

167 Views Asked by At

In my iOS app, I play music and I want to display playback controls on the lock screen, allowing the user to control the audio player.

So following this tutorial and this tutorial, here is what I tried with MPRemoteCommandCenter:

    override func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool
    {
        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge]) { [self] (granted, error) in
            if (granted)
            {
                self.initNotification(application)
            }
            else
            {
                print("Notifications permission denied because: \(error?.localizedDescription).")
            }
        }

        // ...

        return super.application(application, didFinishLaunchingWithOptions: launchOptions)
    }

    private func initNotification(_ application: UIApplication)
    {
        application.beginReceivingRemoteControlEvents()
        MPRemoteCommandCenter.shared().playCommand.isEnabled = true
        MPRemoteCommandCenter.shared().playCommand.addTarget { event in
            return .success
        }
        
        var nowPlayingInfo = [String: Any]()
        nowPlayingInfo[MPMediaItemPropertyTitle] = "Movie Title"
        nowPlayingInfo[MPMediaItemPropertyArtist] = "Artist Name"
        MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
    }

It compiles and run, but for some reason, nothing appears on the lock screen or in the notification center.

What did I forget?

Thanks.

0

There are 0 best solutions below