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

后端 未结 4 2006
半阙折子戏
半阙折子戏 2021-02-19 00:37

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

4条回答
  •  迷失自我
    2021-02-19 00:58

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

提交回复
热议问题