Search code examples
macoscocoadownloadmp3hyperlink

Downloading an mp3 from link inside app


i'm making an mac app that downloads an mp3 from a link.
For example, this link: http://media.soundcloud.com/stream/VGGUdzU69Ng5?stream_token=2U9W2
As you can see, it is an mp3 file. How can i download it to a specific path?
Thank you


Solution

  • The simplest way is to use NSURLDownload:

    NSURL* url = [NSURL URLWithString:@"http://media.soundcloud.com/stream/VGGUdzU69Ng5?stream_token=2U9W2"];
    NSString* destinationPath = [NSHomeDirectory() stringByAppendingPathComponent:@"someFile.mp3"];
    
    NSURLRequest* request = [NSURLRequest requestWithURL:url];
    NSURLDownload* download = [[NSURLDownload alloc] initWithRequest:request delegate:nil];
    [download setDestination:destinationPath allowOverwrite:NO];
    

    Ideally you'd set an object as the delegate so you can receive progress notifications and then release the NSURLDownload object when finished.