开发者

Finding a file in the iPhone sandbox

At the beginning of my app the user records their voice which is then saved as 'recorded.wav' in the sandbox. How do I later create a string indicating it's location? I w开发者_开发知识库ould like something that looks like:

/var/folders/u6/u6pKbm2MFriwajkdt-OQME+++TI/-Tmp-/recorded.wav

Thanks,

Keep in mind this is for the iPhone device and simulator.


NSHomeDirectory() returns the path to your application's sandbox. You can then reach any file or directory within it.

NSString *home = NSHomeDirectory();
NSString *documentsPath = [home stringByAppendingPathComponent:@"Documents"];

There is also NSTemporaryDirectory() which returns the path to the temp folder specific to your app. Do note, that when running your app in the Simulator, NSTemporaryDirectory() will return a system-wide directory.


If you like a more general solution, lookup the documentation on NSSearchPathForDirectoriesInDomains() function.


You can get the path to your file via the NSSearchPathForDirectoriesInDomains foundation function as such:

// Get our document path.
NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath = [searchPaths objectAtIndex:0];

// Get the full path to our file.
NSString *samplePath = [documentPath stringByAppendingPathComponent:@"recorded.wav"];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜