views:

215

answers:

1

Hi folks. Does anyone know how to obtain the path to the current directory when running xcodebuild (mainly, the project directory) and write files to it? I'm trying to save the results of some unit tests to disk from my obj-c code, but I'd rather not dump them deep in the app's document path if possible. I might be able to append '../../..' etc. to the document path, but I was hoping there might be a more elegant method.

*This is relevant only to the simulator, of course... but perhaps even the simulator attempts to maintain the iPhone file system sandbox? I've had no luck writing to other random locations on my Mac, so I suspect that might be the case.

Thanks for your input!

A: 

I figured this out - the answer is embarrassingly simple. With an emphasis on "embarrassing".

If you use something like NSString's writeToFile methods, all you need to do is think of your path as relative to wherever you're running xcodebuild from - no NSSearchPathForDirectoriesInDomains needed.

Example:

(if running in ~/src/awesomeapp)

[myString writeToFile:@"awesome.file" atomically:YES encoding:NSUTF8StringEncoding error:&error];

Will output to: ~/src/awesomeapp/awesome.file

Yes, I am an idiot. I actually tried this first, but I was attempting to write to a relative directory that didn't exist, thus resulting in an unhelpful "Cannot do this" style error that led me all around the internets and eventually to stackoverflow... (you need to use FileManager#createDirectoryAtPath! There is no automatic path creation, unfortunately.)

FYI, as should be apparent, you are free to write files wherever you wish on your hard drive when running via xcodebuild - no sandboxing occurs. If you run in the simulator, though, it might be a different story.

Mike Laurence