有没有办法从 Xcode 将命令行选项传递给我的 iOS 应用程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6272670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:12:48  来源:igfitidea点击:

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

xcodeiosuiapplicationdelegate

提问by Charles Randall

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.

我希望找到一种方法,在测试期间启动应用程序时将某些信息传递给我的应用程序,以便我可以执行特殊的调试任务。Xcode 有一个“启动时传递的参数”部分,我假设它们会出现在我的 UIApplicationDelegate 的应用程序中:didFinishLaunchingWithOptions: 但传入的字典始终为零。

Am I going about this the wrong way?

我会以错误的方式解决这个问题吗?

回答by Deepak Danduprolu

You can access them using NSProcessInfoobject like this,

您可以使用这样的NSProcessInfo对象访问它们,

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

回答by Mario

Another easier way is to use the NSUserDefaults.

另一种更简单的方法是使用 NSUserDefaults。

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

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

From the article:

从文章:

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

-name value

The argument is stored as a default with key of nameand 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"];

可以解析和使用的命令行参数 NSArgumentDomain必须采用以下格式:

-name value

参数存储为默认值,键为name,值为 value。此时,访问命令行上传入的值与访问任何其他默认值的过程相同。

例如运行这样的应用程序:

MyApplication -aString "Hello, World" -anInteger 10

允许像这样检索命令行参数:

NSUserDefaults *standardDefaults = [NSUserDefaults standardUserDefaults];
NSString *aString = [standardDefaults stringForKey:@"aString"];
NSInteger anInteger = [standardDefaults integerForKey:@"anInteger"];

回答by karim

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

对于那些像我一样偶然发现这个问题的人:) 我想logLevel为我的静态库创建一个。我的做法是,

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

回答by robert

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

除了标量,命令行参数可以是 NSData、NSArray 或 NSDictionary 引用。Apple 关于“旧式 ASCII 属性列表”的文档说明了如何操作。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:

例如,此语法应解码为 NSDictionary:

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

MyApplication -aLocation "{ 纬度 = 37.40089; 经度 = -122.109428; }"