Apple Music API - 创建播放列表

Apple Music API - Create a Playlist

我一直在探索 Apple Music API 以了解我希望能够在 iOS 应用程序中使用什么样的功能。我创建了一个小测试应用程序,它获得了用户的许可并将我拥有的播放列表(和歌曲)输出到 NSLog

MPMediaQuery *myPlaylistsQuery = [MPMediaQuery playlistsQuery];
            [myPlaylistsQuery setGroupingType:MPMediaGroupingPlaylist];
            NSArray *playlists = [myPlaylistsQuery collections];
            
            for (MPMediaPlaylist *playlist in playlists) {
                NSLog (@"%@", [playlist valueForProperty: MPMediaPlaylistPropertyName]);
                
                NSArray *songs = [playlist items];
                
                for (MPMediaItem *song in songs) {
                    NSString *songTitle =
                    [song valueForProperty: MPMediaItemPropertyTitle];
                    NSLog (@"\t\t%@", songTitle);
                }
            }

据此,我已经能够推断出以下内容(但我不是 100% 确定):

到目前为止,还不错。我想知道的是:

我知道有 MPMediaPlaylist addItemadd 方法,但似乎找不到创建新播放列表的方法。

根据这个页面应该可以:https://affiliate.itunes.apple.com/resources/blog/apple-music-api-faq/

Can a developer create brand new playlists on the user’s device with the Apple Music API?

Yes. The API allows develops to new create playlists on the user’s device.

我已经弄明白了。如果您使用以下代码,您可以生成一个新的播放列表并对其执行操作。

NSUUID *uuid = [NSUUID UUID]; //uuid for the playlist
[[MPMediaLibrary defaultMediaLibrary] getPlaylistWithUUID:uuid creationMetadata:[[MPMediaPlaylistCreationMetadata alloc] initWithName:@"YOUR PLAYLIST NAME"] completionHandler:^(MPMediaPlaylist * _Nullable playlist, NSError * _Nullable error) {
    NSLog(@"%@", error);

    if (!error) {
        NSLog(@"All ok let's do some stuff with the playlist!"); 
    }
}];

Apple的文档总体上API严重缺乏示例代码和实际例子!