iosswiftmpnowplayinginfocenter

How to update MPNowPlayingInfoCenter in Swift?


In objective c, I've been using code like this to update MPNowPlayingInfoCenter:

[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo: 
    @{ MPMediaItemPropertyArtist : @"Artist!",
        MPMediaItemPropertyTitle : @"Title! }];

But in Swift, it doesn't seem like the function "setNowPlayingInfo" is recognized:

MPNowPlayingInfoCenter.defaultCenter()....  // Can't identify 'setNowPlayingInfo()'

Is there anything I'm missing?


Solution

  • In Swift, getters/setters work differently. Since there are no more properties like in ObjC, there are no automatically generated setters/getters for you. You should just access the variable directly.

    In your case, use:

    MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo = [MPMediaItemPropertyArtist : "Artist!",  MPMediaItemPropertyTitle : "Title!"]