Taking decision on command line parameters in a cocoa applications
I have my Cocoa Application, which will be called with or without parameters in the command line.
I would like to take decisions over th开发者_JAVA百科e parameters received on the application, ie, if a special parameter is received I would like to trigger an action on it. Is there anyway for doing this??
Cheers
Sure, your program has a main()
function like any C program. The default one that comes with a new Cocoa project just calls NSApplicationMain()
, but you can do other actions if you'd like.
If you want to easily access the command line information from elsewhere in your program, you can use _NSGetArgv()
, _NSGetArgc()
, _NSGetEnviron()
, and _NSGetProgname()
. They're declared in crt_externs.h
:
extern char ***_NSGetArgv(void);
extern int *_NSGetArgc(void);
extern char ***_NSGetEnviron(void);
extern char **_NSGetProgname(void);
Here's a blog post about these functions, and a link to the NSApplicationMain
documentation.
You might find it easier to access the NSArgumentDomain
in user defaults:
NSDictionary *const args = [[NSUserDefaults standardUserDefaults]
volatileDomainForName:NSArgumentDomain];
This will handle arguments of the form -NSZombieEnabled YES
. Other forms of arguments (such as -NSZombieEnabled=YES
) might be ignored; I haven't tested or looked at the source.
精彩评论