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

后端 未结 4 2022
半阙折子戏
半阙折子戏 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 01:09

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

提交回复
热议问题