Why do you need to do that? If you really want to set the environment variable in your app, do this:
NSString* expanded=[@"~/myappname" stringByExpandingTildeInPath];
setenv("MYAPPDIR",[expanded UTF8String],1);
please disregard the following rants if you really wanted to set the environment variable.
However, never force a user to put your app in a certain directory.
In addition, never save a file in a user-visible non-user-configurable place.
In OS X, applications are made into .app
bundles so that a user can freely move it.
If you need to get the app's current position, just use
NSString*appPath=[[NSBundle mainBundle] bundlePath];
If you'd like to save a big file somewhere, create a directory inside ~/Library/Application Support/
called
~/Library/Application Support/yourappname/
and save files inside. If you just need to save a few data, use NSUserDefaults. It automatically saves the data to
~/Library/Preferences/com.yourcompany.yourapp.plist
and re-loads the content when the app is launched again. Just do
[[NSUserDefaults standardUserDefaults] setObject:@"boo" forKey:@"bar"];
and retrieve, after your app's relaunched, using
NSString* boo=[[NSUserDefaults standardUserDefaults] stringForKey:@"bar"];
etc.