Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I read a environmental variable in Cocoa?

How could I read an environmental variable that a user has set?

I'm new to desktop development on the Mac (cocoa), and I am building a little tool that I can use to access amazon's s3 service.

I set my environmental variables in my .bash_profile, but I want this to work regardless of where the user entered it (.bashrc, .bash_profile or .profile etc).

like image 829
loyalflow Avatar asked Jul 18 '12 21:07

loyalflow


People also ask

How do I find the value of an environment variable?

On Windows In the command window that opens, enter echo %VARIABLE%. Replace VARIABLE with the name of the environment variable you set earlier. For example, to check if MARI_CACHE is set, enter echo %MARI_CACHE%. If the variable is set, its value is displayed in the command window.

What are the 3 environmental variables?

Let's look at three of the main environmental variables that can influence perception: physiological, psychological, and social.

What is meant by an environmental variable?

An environment variable is a variable whose value is set outside the program, typically through functionality built into the operating system or microservice. An environment variable is made up of a name/value pair, and any number may be created and available for reference at a point in time.


2 Answers

Look at the environment method on a NSProcessInfo. It returns a NSDictionary of the environment so e.g. for PATH

NSString* path = [[[NSProcessInfo processInfo]environment]objectForKey:@"PATH"];
like image 106
mmmmmm Avatar answered Oct 22 '22 12:10

mmmmmm


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

converting to NSString: modern obj-c:

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

legacy obj-c:

NSString *envVarString = [NSString stringWithUTF8String: getenv("__MY_ENV_NAME__")];
like image 41
Radif Sharafullin Avatar answered Oct 22 '22 13:10

Radif Sharafullin