I have read a lot about accessing files in an iPhone app, but I cannot figure out how to get them there in the first place. I have many sound files that are important to my app, and many more that can be purchased using IAP. I know that these files should go in my /Library/Application Support/ directory, however I cannot figure out how to get them there. How do I do this? I am sure that there is something incredibly obvious that I'm missing, but I can't figure it out. Also, in my app there is a UITableView that keeps track of these files. What is the best way to keep that table's datasource up-to-date as the user purchases new sounds? I would assume keeping an array in my model would be best, but I'm not sure how to read and write the contents of an array to disk. Any help is appreciated.

有帮助吗?

解决方案

You can use this code to create your folder and then save your files there.

NSArray *filePaths =    NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask,YES); 
NSString *path=[filePaths objectAtIndex: 0];
NSString *tempPath=[[NSString alloc]initWithFormat:@"%@/%@",path,@"TempFolder"];

BOOL bDir;
NSError *error;
if([[NSFileManager defaultManager] fileExistsAtPath:tempPath isDirectory:&bDir]==FALSE){
    [[NSFileManager defaultManager] createDirectoryAtPath:tempPath withIntermediateDirectories:YES attributes:nil error:&error];
}

Then use this code to save your files in your folder

NSArray *filePaths =    NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask,YES);
NSString *pathAndName=[[NSString alloc] initWithFormat:@"%@/TempFolder/%@",
                       [filePaths objectAtIndex: 0],
                       fileName];
NSData * data = //Load your file here
[data writeToFile:pathAndName atomically:YES];
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top