答案 0 :(得分:5)
你可以写下这样的东西:
//Download data
NSData *data = [NSData dataWithContentsOfURL:<YOUR URL>];
//Find a cache directory. You could consider using documenets dir instead (depends on the data you are fetching)
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
//Save the data
NSString *dataPath = [path stringByAppendingPathComponent:@"filename"];
dataPath = [dataPath stringByStandardizingPath];
BOOL success = [data writeToFile:dataPath atomically:YES];
答案 1 :(得分:0)
您可以使用NSURLConnection
将文件下载到内存,然后将其写入文件。
[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:yourURL]
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
[data writeToFile:yourPath atomically:YES];
// Audio file is ready to be played.
}];
您还可以创建自己的NSOperationQueue
实例,以便在后台执行写作。
如果文件很大并且您不想将其整体保留在内存中,则应使用AFNetworking
,或者您也可以使用NSOutputStream
编写自己的实现来直接写入磁盘。< / p>