【问题标题】:Is there a way to pass command line options to my iOS app from Xcode?有没有办法从 Xcode 将命令行选项传递给我的 iOS 应用程序?
【发布时间】:2011-09-10 11:43:45
【问题描述】:

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

我是不是走错路了?

【问题讨论】:

    标签: xcode ios uiapplicationdelegate


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

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

      来自文章:

      可以被解析和使用的命令行参数 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"];
      

      【讨论】:

        【解决方案3】:

        对于像我这样偶然发现这个问题的人:) 我想为我的静态库创建一个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);
        }
        

        【讨论】:

          【解决方案4】:

          除了标量,命令行参数还可以是 NSData、NSArray 或 NSDictionary 引用。 Apple 关于“旧式 ASCII 属性列表”的文档告诉了如何做到这一点。 https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/PropertyLists/OldStylePlists/OldStylePLists.html#//apple_ref/doc/uid/20001012-BBCBDBJE

          例如,这个语法应该解码成一个 NSDictionary:

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2018-05-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2010-11-09
            • 1970-01-01
            • 2011-02-08
            相关资源
            最近更新 更多