2016-02-12 7 views
0

Voici ce que j'ai essayé jusqu'à présent: - (IBAction) getPlaylistsButtonTapped: (id) sender {Obtenir un playlists de l'utilisateur et d'être en mesure de choisir une chanson de lui

NSURLRequest *playlistrequest = [SPTPlaylistList createRequestForGettingPlaylistsForUser:@"charleshyowonkang" withAccessToken:_accessToken error:nil]; [[SPTRequest sharedHandler] performRequest:playlistrequest callback:^(NSError *error, NSURLResponse *response, NSData *data) { 
    if (error != nil) { NSLog(@"error"); 
    } 
    SPTPlaylistList *playlists = [SPTPlaylistList playlistListFromData:data withResponse:response error:nil]; 
    NSLog(@"Got possan's playlists, first page: %@", playlists); 
    NSURLRequest *playlistrequest2 = [playlists createRequestForNextPageWithAccessToken:_accessToken error:nil]; 

    [[SPTRequest sharedHandler] performRequest:playlistrequest2 callback:^(NSError *error2, NSURLResponse *response2, NSData *data2) { 
     if (error2 != nil) { 
      NSLog(@"error2"); 
     } 
     SPTPlaylistList *playlists2 = [SPTPlaylistList playlistListFromData:data2 withResponse:response2 error:nil]; 
     NSLog(@"Got possan's playlists, second page: %@", playlists2); 
    }];}]; 

}

Tous les tutoriels/messages stackoverflow que j'ai googlé sont pour des projets web, mais j'essaye de le faire sous iOS. Je n'ai vraiment besoin de faire une demande à l'API que deux fois.

1) lorsqu'un utilisateur se connecte avec son compte spotify

2) lorsqu'un utilisateur appuie sur le bouton « sons » et il tire leurs listes de lecture/pistes.

Toute aide iOS serait très utile.

Répondre

0

J'utilise ce qui suit pour obtenir les albums et essayer de prendre une chanson de la liste de lecture. Cela fonctionne bien.

[SPTRequest playlistsForUserInSession:auth.session callback:^(NSError *error, SPTListPage *list){ 
     if (error != nil) { 
      UIAlertView *view = [[UIAlertView alloc] initWithTitle:@"Getting User Info Failed" 
                  message:error.userInfo[NSLocalizedDescriptionKey] 
                  delegate:nil 
               cancelButtonTitle:@"OK" 
               otherButtonTitles:nil]; 
      [view show]; 
      return; 
     } 

     //[spotifyPlaylistsArr addObjectsFromArray:list.items]; 

     [self getFullPlaylistPage:list]; 

    }]; 

-(void)getFullPlaylistPage:(SPTListPage*)listPage { 

    if (listPage.hasNextPage) { 
     [listPage requestNextPageWithSession:[SPTAuth defaultInstance].session callback:^(NSError *error, SPTListPage* playlistPage) { 
      if (error != nil) { 
       NSLog(@"*** Getting playlist page got error: %@", error); 
       return; 
      } 

      SPTListPage *listPage2 = [listPage pageByAppendingPage:playlistPage]; 
      [self getFullPlaylistPage:listPage2]; 
     }]; 
    } else { 

     NSMutableArray* playlist = [[NSMutableArray alloc]init]; 
     [self convertPlaylists:listPage arrayOfPlaylistSnapshots:playlist positionInListPage:0]; 
    } 
} 

-(void)convertPlaylists:(SPTListPage*)playlistPage arrayOfPlaylistSnapshots:(NSMutableArray*)playlist positionInListPage:(NSInteger)position { 

    if (playlistPage.items.count > position) { 
     SPTPartialPlaylist* userPlaylist = playlistPage.items[position]; 
     [SPTPlaylistSnapshot playlistWithURI:userPlaylist.uri session:[SPTAuth defaultInstance].session callback:^(NSError *error, SPTPlaylistSnapshot* playablePlaylist) { 

      if (error != nil) { 
       NSLog(@"*** Getting playlists got error: %@", error); 
       return; 
      } 

      if(!playablePlaylist){ 
       NSLog(@"PlaylistSnapshot from call back is nil"); 
       return; 
      } 

      [playlist addObject:playablePlaylist]; 
      [self convertPlaylists:playlistPage arrayOfPlaylistSnapshots:playlist positionInListPage:position+1]; 

      [spotifyPlaylistsArr removeAllObjects]; 

      [spotifyPlaylistsArr addObjectsFromArray:playlist]; 

      [spotifyPlaylists reloadData]; 

     }]; 
    } 
    else 
    { 

    } 
} 

S'il vous plaît laissez-moi savoir si vous avez besoin de plus de détails.