How can I read the environment variable in Cocoa?

How can I read the environment variable that the user set?

I am new to Mac desktop development (cocoa) and I am creating a small tool that I can use to access the amazon s3 service.

I set my environment variables to my .bash_profile, but I want it to work no matter where the user entered it (.bashrc, .bash_profile or .profile, etc.).

+8
objective-c cocoa
source share
2 answers

Take a look at the environment method in NSProcessInfo. It returns an NSDictionary environment, for example for PATH

NSString* path = [[[NSProcessInfo processInfo]environment]objectForKey:@"PATH"]; 
+17
source share

You can use the C API from the GNU library http://www.gnu.org/software/libc/manual/html_node/Environment-Access.html#Environment-Access

conversion to NSString: modern obj-c:

 NSString *envVarString = @(getenv("__MY_ENV_NAME__")); 

legacy obj-c:

 NSString *envVarString = [NSString stringWithUTF8String: getenv("__MY_ENV_NAME__")]; 
+5
source share

All Articles