Asynchronously Downloading MP3s

Discussion in 'iOS Programming' started by newtoiphonesdk, Mar 12, 2012.

  1. macrumors 6502a

    Joined:
    Jul 30, 2010
    #1
    I am having issues getting this accomplished correctly. I have a Table View Controller that parses the XML of the podcast loaded with MP3s. On my did select row, I have:
    Code:
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    RSSEntry *entry = [_allEntries objectAtIndex:indexPath.row];
        NSURL *url = [NSURL URLWithString:entry.articleUrl];    
            NSLog(@"%@", url);
    NSURLRequest *theRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
    receivedData = [[NSMutableData alloc] initWithLength:0];
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self startImmediately:YES];
    }
    The only warning that I get is that connection is unused, but I am trying to set it up the same way the Apple documentation has it. I then have the following methods implemented:
    Code:
    - (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
        [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
        [receivedData setLength:0];
    }
    
    - (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
        [receivedData appendData:data];
    }
    
    - (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        [connection release];
    }
    
    - (NSCachedURLResponse *) connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse {
        return nil;
    }
    
    - (void) connectionDidFinishLoading:(NSURLConnection *)connection {
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        [connection release];
    }
    
    When I click on the row, the NSLog returns
    Code:
    2012-03-12 12:33:42.154 SMCoC[11283:17f03] http://www.smcoc.net/content/audio/sermons/2012/2012_03_11am_the_big_hunt.mp3#!111433318
    
    When I click the row, networkActivityIndicator starts spinning, and goes for a little while and then stops. I also have a Second Table View for the Saved MP3s, where files is a NSMutableArray:
    Code:
    - (void)viewWillAppear:(BOOL)animated {
    	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    	NSString *documentsDirectoryPath = [paths objectAtIndex:0];
    	NSFileManager *manager = [NSFileManager defaultManager];
    	self.title = @"Saved Sermons";
    	self.files = [[manager contentsOfDirectoryAtPath:documentsDirectoryPath error:nil] retain];
    	
    	
    	
    	self.navigationItem.rightBarButtonItem = self.editButtonItem;
    	[self.tableView reloadData];
        [super viewDidLoad];
        [super viewWillAppear:animated];
    	
    }
    
    And for the Rows in Table:
    Code:
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        // Return the number of rows in the section.
        return [self.files count];
    }
    
    
    // Customize the appearance of table view cells.
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    	
    	
    	
    	
    	
        static NSString *CellIdentifier = @"Cell";
    	
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        }
    	
    	cell.textLabel.text = [self.files objectAtIndex:indexPath.row];
    	UIFont *cellFont = [UIFont fontWithName:@"AmericanTypewriter-Bold" size:17];    
        cell.textLabel.font = cellFont;
       	return cell;  
    }
    However, when I go to that Saved Table View, nothing appears. Am I doing something obviously wrong that the file is not actually downloading?
     
  2. macrumors member

    Joined:
    Sep 8, 2008
    #2
    I believe you forgot one key piece: when the data is done downloading, you need to write to the device.

    add:
    Code:
    [receivedData writeToFile:localFilename atomically:YES];
    to the connectionDidFinishLoading method.
     
  3. newtoiphonesdk, Mar 12, 2012
    Last edited: Mar 12, 2012

    thread starter macrumors 6502a

    Joined:
    Jul 30, 2010
    #3
    Of course...wow. One other question. Some of these files are quite large, and can take a bit of time. I will be learning to add a UIProgressView, but had a question first about download limits. I know that in the Review guidelines, apple states that when streaming stuff, it can't download more than 5 MB in 5 minutes. Does the same rule apply for Asynchronous downloads using NSURLConnection? If so, how in the world are we to download anything over 5MB on a cell connection?
     

Share This Page