Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to pass command line options to my iOS app from Xcode?

I'm hoping to find a method to pass certain information in to my app when I launch it during testing, so that I can perform special debug tasks. Xcode has a section "Arguments Passed on Launch", and I assumed they would show up in my UIApplicationDelegate's application:didFinishLaunchingWithOptions: but the dictionary that's passed in is always nil.

Am I going about this the wrong way?

like image 490
Charles Randall Avatar asked Jun 07 '11 23:06

Charles Randall


People also ask

Is there a command line for iOS?

Terminal is a sandboxed command line environment for iOS that has over 30 commands currently available, covering many of the most used command line tools and commands you know and love, like cat, grep, curl, gzip and tar, ln, ls, cd, cp, mv, rm, wc, and more, all available right on your iPhone or iPad.

What is Xcodebuild?

xcodebuild builds one or more targets contained in an Xcode project, or builds a scheme contained in an Xcode workspace or Xcode project. Usage To build an Xcode project, run xcodebuild from the directory containing your project (i.e. the directory containing the projectname. xcodeproj package).


3 Answers

You can access them using NSProcessInfo object like this,

NSArray * arguments = [[NSProcessInfo processInfo] arguments];
like image 50
Deepak Danduprolu Avatar answered Sep 18 '22 05:09

Deepak Danduprolu


Another easier way is to use the NSUserDefaults.

http://perspx.com/archives/parsing-command-line-arguments-nsuserdefaults/

From the article:

Command line arguments that can be parsed and used by the NSArgumentDomain must take the format:

-name value

The argument is stored as a default with key of name and value of value. At this point accessing values passed in on the command line is the same process for accessing any other defaults.

For example running an application as such:

MyApplication -aString "Hello, World" -anInteger 10

allows the command line arguments to be retrieved as such:

NSUserDefaults *standardDefaults = [NSUserDefaults standardUserDefaults];
NSString *aString = [standardDefaults stringForKey:@"aString"];
NSInteger anInteger = [standardDefaults integerForKey:@"anInteger"];
like image 40
Mario Avatar answered Sep 19 '22 05:09

Mario


For those who stumbled to this question like me :) I wanted to have a logLevel for my static lib. The way I did is,

static NSUInteger logLevel = 1;
/** This argument should be passed from XCode's build scheme configuration option, Arguments passed on launch */
static const NSString *kIdcLogLevelArgument = @"-com.mycompany.IDCLogLevel";

@implementation IDCLogger

+ (instancetype)sharedInstance {
    static id sharedInstance = nil;

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[self alloc] init];
    });

    return sharedInstance;
}

+(void)initialize
{
    logLevel = 1;
    NSArray *arguments = [[NSProcessInfo processInfo] arguments];
    NSUInteger value = 0;

    if ([arguments containsObject:kIdcLogLevelArgument]) {
        NSUInteger index = [arguments indexOfObject:kIdcLogLevelArgument];
        if (arguments.count > index) {
            NSString *valueStr = [arguments objectAtIndex:index + 1];
            NSCharacterSet* notDigits = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
            if ([valueStr rangeOfCharacterFromSet:notDigits].location == NSNotFound)
            {
                value = [valueStr integerValue];
                logLevel = value;
            }
        }
    }
    NSLog(@"%@:logLevel = %lu", [self class], (unsigned long)logLevel);
}

+ (void)setLogLevel:(NSUInteger)l
{
    logLevel = l;
    NSLog(@"[%@]: Log level set to: %lu", [self class], (unsigned long)l);
}
like image 33
karim Avatar answered Sep 18 '22 05:09

karim