Setting up app lunch argument using ProcessInfo.processInfo.arguments not working in iOS 18

Hi,

I'm trying to set up FIRDebugEnabled as launch arguments in app delegate based on user choice using ProcessInfo.processInfo.arguments. Which is working fine in iOS 17 and below devices and stoped working in iOS 18

Here is my sample,

if condition {
  var arguments = ProcessInfo.processInfo.arguments
                arguments.append("-FIRDebugEnabled")
  ProcessInfo.processInfo.setValue(arguments, forKey: "arguments")
}
Answered by DTS Engineer in 812112022

Oh gosh, don’t do that!

The environment property is read-only and you’re bypassing that using key-value-coding (KVC). That sort of thing is not supported, and this is a good example as to why. Over the last few years we’ve been steadily rewriting Foundation in Swift [1], and that rewrite doesn’t support the implementation detail you were relying on.

The correct way to set an environment variable is with setenv. For example, this code:

print("before, version: \(UIDevice.current.systemVersion)")
print(ProcessInfo.processInfo.environment["foo"] ?? "-")
print(getenv("foo").map { String(cString: $0) } ?? "-")

setenv("foo", "bar", 1)

print("after")
print(ProcessInfo.processInfo.environment["foo"] ?? "-")
print(getenv("foo").map { String(cString: $0) } ?? "-")

works on iOS 17:

before, version: 17.5
-
-
after
bar
bar

and 18:

before, version: 18.0
-
-
after
bar
bar

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] If you’re curious, watch the first half of Swift & Interoperability.

https://youtu.be/**6C_XEv1Mo

Accepted Answer

Oh gosh, don’t do that!

The environment property is read-only and you’re bypassing that using key-value-coding (KVC). That sort of thing is not supported, and this is a good example as to why. Over the last few years we’ve been steadily rewriting Foundation in Swift [1], and that rewrite doesn’t support the implementation detail you were relying on.

The correct way to set an environment variable is with setenv. For example, this code:

print("before, version: \(UIDevice.current.systemVersion)")
print(ProcessInfo.processInfo.environment["foo"] ?? "-")
print(getenv("foo").map { String(cString: $0) } ?? "-")

setenv("foo", "bar", 1)

print("after")
print(ProcessInfo.processInfo.environment["foo"] ?? "-")
print(getenv("foo").map { String(cString: $0) } ?? "-")

works on iOS 17:

before, version: 17.5
-
-
after
bar
bar

and 18:

before, version: 18.0
-
-
after
bar
bar

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] If you’re curious, watch the first half of Swift & Interoperability.

https://youtu.be/**6C_XEv1Mo

Hi @DTS Engineer,

Thank you for the suggestion. Setting up environment is working fine in both iOS versions. I'd like to see if there is similar way to pass launch arguments. As per Firebase we need to inject -FIRDebugEnabled as a launch argument. Appreciate your help in advance.

Setting up app lunch argument using ProcessInfo.processInfo.arguments not working in iOS 18
 
 
Q