Change iOS UserDefaults

I am trying to change the UserDefaults (not NSUserDefaults) of an iOS app I’m testing before launching the app. I’ve looked online for possible solutions to it, and the one that I think is feasible is using the “processArguments” capability to set the UserDefaults. I’m having trouble setting the UserDefaults using this method, so I was wondering what the correct syntax is to implement this. Thank you!

I’ve tried that method using both the arguments method and the environment method. But it doesn’t seem like the variables are being passed.

Arguments Method

JSONObject argsValue = new JSONObject();
List processArgs = new ArrayList();
processArgs.add(“first sign in”);
processArgs.add(“true”);
processArgs.add(“first onscreen time”);
processArgs.add(“true”);
argsValue.put(“args”, processArgs);
desiredCapabilities.setCapability(“processArguments”, argsValue.toString());

Environments Method

JSONObject argsValue = new JSONObject();
Map<String, Object> processEnv = new HashMap<>();
processEnv.put(“first sign in”, “true”);
processEnv.put(“first onscreen time”, “true”);
argsValue.put(“env”, processEnv);
desiredCapabilities.setCapability(“processArguments”, argsValue.toString());

“first sign in” and “first onscreen time” are the names of the “keys” that are in the app I am testing. I’m not sure if the spaces between the keys could be causing the issue.

Setup

  • Appium Server v1.7.2
  • Testing real device iPhone 6S running iOS v11.4.1

yes, spaces might cause the issue. Try to replace them with underscores

also command line argument names are prefixed with dashes as a rule

The app that I’m testing does not pass in any launch arguments or contain any environment variables. Is there special syntax to set the UserDefaults? I’m not having any luck with the “processArguments” capability.

Only the app itself can manage its user defaults. You might patch it to support defaults modification via providing command line arguments or env variables, otherwise there is no other way to do that: https://stackoverflow.com/questions/8939228/nsuserdefaults-is-it-possible-to-get-userdefaults-from-another-app

Bummer! I’ll have to get launch arguments or environment variables implemented on the iOS app I’m testing.