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

孤街醉人 提交于 2019-12-04 00:33:51

You can access them using NSProcessInfo object like this,

NSArray * arguments = [[NSProcessInfo processInfo] arguments];
Mario

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"];

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);
}

In addition to scalars, command line arguments can be an NSData, NSArray, or NSDictionary references. Apple's documentation on "Old-Style ASCII Property Lists" tells how to do it. https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/PropertyLists/OldStylePlists/OldStylePLists.html#//apple_ref/doc/uid/20001012-BBCBDBJE

For example, this syntax should decode into an NSDictionary:

MyApplication -aLocation "{ latitude = 37.40089; longitude = -122.109428; }"

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!