Getting Current Directory in Objective C

Viewed 23770

I am trying to get current directory, but its giving me path of DEBUG folder, how i can get the path of current directory. I am using the following code.

NSFileManager *filemgr;
NSString *currentpath;

filemgr = [[NSFileManager alloc] init];

currentpath = [filemgr currentDirectoryPath];
2 Answers

Developing further on that. The current working directory and launch directory may be different! Take this example, the app is launched from the home directory (~).

Library/Developer/Xcode/DerivedData/MyApp-ehwczslxjxsxiob/Build/Products/Debug/MyApp.app/Contents/MacOS/MyApp -param value
NSString *p1 = [[NSFileManager defaultManager] currentDirectoryPath];
// p1 = ~/Library/Containers/com.MyApp.beta/Data
NSString *p2 = [[NSProcessInfo processInfo] environment][@"PWD"];
// p2 = ~

Depending on the situation you might want one or the other. In my case I want to provide a CLI which takes path arguments that may be relative to the current working directory in the shell.

Related