Spotify iOS-SDK:从播放列表中获取曲目返回(null)曲目

时间:2016-10-09 10:40:53

标签: ios objective-c arrays xcode spotify

我想通过Spotify iOS SDK从选定的播放列表中获取曲目。 但它总是在我的数组中没有返回任何项目。 我正在使用以下代码来获取曲目:

[SPTPlaylistSnapshot playlistWithURI:selectedPlaylist.uri accessToken:auth.session.accessToken callback:^(NSError *error, id object) {

    if (!error) {
        SPTPlaylistSnapshot *playlistSnapchot = object;

        NSArray *songItemsArray =  playlistSnapchot.firstTrackPage.items;
        NSLog(@"LOADED: %@", songItemsArray);

        [songItemsArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

            SPTPartialTrack *track = obj;
            NSLog(@"%@", track.name);


        }];

    }
    else
    {
        NSLog(@"Error loading Playlist Tracks: %@", error.localizedDescription);
    }

}];

每次 songsItemsArray (null)

有谁知道这个问题的原因是什么? 我正在使用所有Spotify Scopes。

1 个答案:

答案 0 :(得分:2)

这可能来自缺少链接器标志,添加" -all_load"项目中的其他链接器标志。

我在我的应用中执行此操作,这是我的代码:

func getTracksFrom(page:SPTListPage, allItems:[SPTPlaylistTrack]) -> [SPTPlaylistTrack] {
    guard let items = page.items as? [SPTPlaylistTrack] else {print("empty page");return allItems}
    var allTracks = allItems
    allTracks.append(contentsOf: items)

    var nextPage = SPTListPage()

    if page.hasNextPage {
        page.requestNextPage(withAccessToken: (SPTAuth.defaultInstance().session.accessToken)!) { (error, data) in
            guard let p = data as? SPTListPage else {return}
            nextPage = p
        }

        return getTracksFrom(page:nextPage,allItems:allTracks)
    }
    return allTracks
}

func getAllTracksFrom(_ playlist:SPTPlaylistSnapshot) -> [SPTPlaylistTrack]  {
    var allTracks:[SPTPlaylistTrack] = []
    allTracks = getTracksFrom(page: playlist.firstTrackPage, allItems: allTracks)
    return allTracks
}

override func viewDidLoad() {
    super.viewDidLoad()
    guard let p = playlist else {return}
    SPTPlaylistSnapshot.playlist(withURI: p.uri, accessToken: (SPTAuth.defaultInstance().session.accessToken)!, callback: { (error, dataOrNil) in
        guard error == nil else {print(error!);return}
        guard let playlistSnapshot = dataOrNil as? SPTPlaylistSnapshot else {print("couldn't cast as SPTPlaylistSnapshot");return}
        self.tracks = self.getAllTracksFrom(playlistSnapshot)
        self.tableView.reloadData()
    })
}