Yes, you can.
You can use a NSURLConnection
and save the received data into a temporary NSData variable, and when done, write it to disk.
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
_mutableData = [NSMutableData new];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
if (_mutableData) {
[_mutableData appendData:data];
}
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
dispatch_queue_t bgGlobalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);
dispatch_async(bgGlobalQueue, {
[_mutableData writeToFile:_filePath atomically:YES];
});
}
Note: you should add all the corresponding error handling to the above code, do not use it "as is".
You can then create a NSURL
with the file path and use that URL to play the mp3 file.
NSURL *url = [NSURL fileURLWithPath:_filePath];