@interface SpotifyTrack : NSObject
@property (nonatomic, strong) NSString *id;
@property (nonatomic, strong) NSString *artist;
@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSURL *thumbnailUrl;
@end
- (void)viewDidLoad
{
[super viewDidLoad];
SpotifyTrack *track = [[SpotifyTrack alloc] init];
track.id = @"";
track.title = @"";
self.titleLabel.text = track.title;
}
- (IBAction)openInSpotifyButtonTouch:(id)sender {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"spotify://%@", self.track.id]]];
}
SpotifyTrack track1 = [SpotifyTrack alloc] init];
track1.id = @"1"
track1.title = @"Title";
track1.artist = @"Artist";
SpotifyTrack track2 = [SpotifyTrack alloc] init];
track1.id = @"1"
track1.title = @"Title";
track1.artist = @"Artist";
self.tracks = @[track1, track2];
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.tracks.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"trackCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
SpotifyTrack *track = self.tracks[indexPath.row];
cell.textLabel.text = track.title;
cell.detailTextLabel.text = track.artist;
return cell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
SpotifyPlayerViewController *viewController = (SpotifyPlayerViewController *)[segue destinationViewController];
viewController.track = self.tracks[[[self.tableView indexPathForSelectedRow] row]];
}
#import <Foundation/Foundation.h>
@class SpotifyTrack;
typedef void(^KSpotifyTrackSearchResult)(NSArray* tracks);
typedef void(^KSpotifyServiceError)(NSError* error);
@interface SpotifyService : NSObject
+(void)findTracks:(NSString *)title result:(KSpotifyTrackSearchResult)result error:(KSpotifyServiceError)error;
@end
+ (void)findTracks:(NSString *)title result:(KSpotifyTrackSearchResult)result error:(KSpotifyServiceError)error {
NSString *urlString = [NSString stringWithFormat:@"http://ws.spotify.com/search/1/track.json?q=%@", [title stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[KURLConnection startWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]] successHandler:^(NSData *response) {
NSError *convertError = nil;
NSArray *tracks = [SpotifyService convertDataResponseToTracks:response error:&convertError];
if (convertError != nil) {
error(convertError);
}
result(tracks);
} failedHandler:^(NSError *requestError) {
error(requestError);
}];
}
+ (NSArray *)convertDataResponseToTracks:(NSData *)response error:(NSError **)error {
NSError __autoreleasing *jsonError = nil;
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingAllowFragments error:&jsonError];
if (jsonError != nil) {
NSLog(@"Failed parsing json: %@", [jsonError localizedDescription]);
error = &jsonError;
}
NSMutableArray *tracks = [[NSMutableArray alloc] init];
for (NSDictionary *track in dictionary[@"tracks"]) {
[tracks addObject:[SpotifyService convertResponseTrackToTrack:track]];
}
return tracks;
}
+ (SpotifyTrack *)convertResponseTrackToTrack:(NSDictionary *)trackObject {
SpotifyTrack *track = [[SpotifyTrack alloc] init];
track.id = trackObject[@"href"];
track.title = trackObject[@"name"];
track.artist = trackObject[@"artists"][0][@"name"];
return track;
}
- (void) searchBarSearchButtonClicked:(UISearchBar *)searchBar {
[searchBar resignFirstResponder];
[SpotifyService findTracks:searchBar.text result:^(NSArray *tracks) {
self.tracks = tracks;
[self.tableView reloadData];
} error:^(NSError *data) {
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"Feilet" message:@"Feilet skikkelig" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alertView show];
}];
}