2

I have an application that I can't modify (although I have the source code) that uses values from the user default's system ([NSUserDefaults standardUserDefaults]) to configure some settings from the app.

I'm trying to create a command line script that builds the app with xcodebuild, and then launches the app directly into the iPhone simulator or a device. I'd like to be able to modify a value of the "user default's system" so when I lunch the app in either the iPhone simulator or an iPhone device, it'll start with the configuration that I want. How can I do that?

Things I've tried:

  • xcrun simctl spawn <simulator-device-id> defaults read but the settings from my app doesn't appear there.

  • Modifying ~/Library/Developer/CoreSimulator/Devices//data/Containers/Data/Application/<app-id>/Library/Preferences/<app-name>.plist with /usr/libexec/PlistBuddy: This one seems to work partially, but I only can find this folder after I've run the app for the first time, and I haven't find any easy way to know what's the app-id of my app from my command line before actually running the app in the iphone simulator. Ideally, I'd like my app to run for the first time with the settings that I want to use, if I use the PlistBuddy options it seems I'll have to run it once to find the folder, and a second time to actually set the setting before running it.

Is there any easy way to accomplish this?

Cristik
  • 30,989
  • 25
  • 91
  • 127
User
  • 21
  • 3

1 Answers1

1

I think this would accomplish what you want.

If you launch an application in your simulator via xcrun simctl launch <deviceid> <app identifier>, you can provide arguments to the application.

xcrun simctl help launch
Usage: simctl launch [-w | --wait-for-debugger] <device> <app identifier> [<argv 1> <argv 2> ... <argv n>]

If you want to set environment variables in the resulting environment, set them in the calling environment with a SIMCTL_CHILD_ prefix.

The standard example of these parameters is for setting AppleLocale or AppleLanguages. But as long as you put a dash before an argument and follow it with an argument without a dash, that makes the first argument a key in your app's defaults, and the second argument will be the value.

For example, I have this code in my application:didFinishLaunchingWithOptions: method in my app so that all of the defaults get printed:

NSLog(@"Defaults: %@", [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]);

And on the command line I've entered:

xcrun simctl launch booted com.Myapp -AppleLanguages "(ru)" -variableFromCommandLineWithoutParameters -variableFromCommandLineWithParameter "hello"

Looking in the simulator's log, I find:

Jan 25 18:27:46 AaronMacPro MyApp[70005]: Defaults: {

  AddingEmojiKeybordHandled = 1;
  AppleITunesStoreItemKinds =     (
      audiobook,
      "tv-episode",
      booklet,
      software,
      "software-update",
      "itunes-u",
      ringtone,
      "tv-season",
      movie,
      mix,
      wemix,
      song,
      tone,
      artist,
      "podcast-episode",
      podcast,
      eBook,
      document,
      album,
      "music-video"
  );
  AppleKeyboards =     (
      "en_US@hw=US;sw=QWERTY",
      "emoji@sw=Emoji",
      "en_US@hw=US;sw=QWERTY"
  );
  AppleKeyboardsExpanded = 1;
  AppleLanguages =     (
      ru
  );
  AppleLanguagesDidMigrate = "9.1";
  AppleLocale = "en_US";
  ApplePasscodeKeyboards =     (
      "en_US@hw=US;sw=QWERTY",
      "emoji@sw=Emoji",
      "en_US@hw=US;sw=QWERTY"
  );
  NSInterfaceStyle = macintosh;
  NSLanguages =     (
      ru,
      en
  );
  variableFromCommandLineWithParameter = hello;

}

That's a lot of spam, but if you scroll down, you can find the values I set. The AppleLanguages key has been filled in with "ru" instead of the default "en". My variableFromCommandLineWithParameter showed up with its value of "hello". My variableFromCommandLineWithoutParameters did not show up in any way.

I haven't used xcrun simctl spawn like you said you tried in your question, but according to the help text it also allows arguments like this if that's what you need.

Community
  • 1
  • 1
Aaron
  • 3,209
  • 1
  • 12
  • 14