Manus

Xcode

  1. Starte Xcode, Opprette prosjekt, Empty application
  2. Oversikt over XCode
  3. Prosjektinnstillinger
  4. Bygge og kjøre apps

Lage SpotifyPlayerViewController

  1. Lage storyboard og sette main interface + fjerne innhold i AppDelegate
  2. Dra inn en ViewController, knapp og label i Storyboardet
  3. Lage og sette tilhørende ViewController klasse (Hva er *.h og *.m filene)
  4. Knytte interface-elementer til underliggende ViewController, slik at de kan refereres fra kode
  5. Lag SpotifyTrack (Klasser, Arv, imports, @properties (ivars), Objekter har pekere)
    @interface SpotifyTrack : NSObject
    	@property (nonatomic, strong) NSString *id;
    	@property (nonatomic, strong) NSString *artist;
    	@property (nonatomic, strong) NSString *title;
    	@property (nonatomic, strong) NSURL *thumbnailUrl;
    @end
    
  6. Opprett en ny SpotifyTrack i SpotifyPlayerViewController sin viewDidLoad
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        SpotifyTrack *track = [[SpotifyTrack alloc] init];
        track.id = @"";
        track.title = @"";
        self.titleLabel.text = track.title;
    }
    
  7. Knytte knapp til IBAction og forklare message passing (metode kall)
    - (IBAction)openInSpotifyButtonTouch:(id)sender {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"spotify://%@", self.track.id]]];
    }
    

Lage SpotifySearchResultTableViewController

  1. Lage TableViewController og NavigationController i Interface Builder
  2. Forklare Delegates
  3. Implementere statisk array med SpotifyTracks
    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];
    
  4. Implementere DataSource metodene til UITableView delegate
    - (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;
    }
    
  5. Lage property på SpotifyPlayerViewController for track og fjerne fra viewDidLoad
  6. Vise hvordan man kan sende data via navigationController seque
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        SpotifyPlayerViewController *viewController = (SpotifyPlayerViewController *)[segue destinationViewController];
        viewController.track = self.tracks[[[self.tableView indexPathForSelectedRow] row]];
    }
    

Lage SpotifyService

PAUSE
  1. Forklare Klasse metoder og Blocks
    		
    #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;
    }
    
  2. Forklare Error handling
  3. Forklare Blocks
  4. Forklare KURLConnection med threading og NSJSONSerialization
  5. Forklare NSDictionary
  6. Bytt ut statisk array med service
  7. Lag UISearchBar og set opp delegate
    	
    - (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];
        }];
    }