Has anyone had any success detecting if their KMP ...
# multiplatform
j
Has anyone had any success detecting if their KMP code is running on an iOS simulator?
targetEnvironment(simulator)
isn’t callable from KMP.
f
In swift you check on runtime : -> ProcessInfo.processInfo.environnment[“XCODE_RUNNING_FOR_PREVIEW”] != nil
j
Have you tested on a simulator? I figured this would only work for the SwiftUI previews.
f
Oh, the simulator, my bad
j
Also, I’m writing a library and I would like functionality internal to the library to determine if running in simulator so I can’t use pure Swift otherwise I’d use the
targetEnvironment(...)
.
f
So yeah, It’s swift compiler directive
I guess, you can create a swift/objc/kotlin bridge to check this condition
j
Wouldn’t that require the consuming app to pass into some Swift object into my library?
f
but directive is only during the compilation not runtome
(Instant advertisement -> https://spmforkmp.eu/)
j
Not sure how this library would work. Seems like the only way to determine if running on a simulator is to use a compile time flag since the builds for a simulator and real iPhone are different.
Do you have a basic example to achieve it with your library?
f
Copy code
@objcMembers public class MySwiftBridge: NSObject {
    static public func isBuildForSimulator() -> Bool {
        #if targetEnvironment(simulator)
          return true
        #else
          return false
        #endif
    }
}
Or in kmp, you can use subtarget (iosArm64Main & iosSimulatorArm64Main)
with expect/actual
I guess, the last one is better
j
Why do you say the last is better?
f
you don’t need to use a plugin; the kotlin compiler will do the job the expect/actual
j
The latter option has the drawback of effectively having “empty” sourceSets which just return
true
f
you have what you need easily in that way
j
I agree using the default KMP toolset is easy but my concern is my library would be “bloated” with ugly marker source sets 🤣… I will look review your library/plugin and investigate what methods works better for me.
Thanks for the help
🙌 1
Declaring separate source sets for iOS simulators is very laborious because all my expect declarations will need actual implementations. It would be very nice if I could just override certain behavior.
f
Or, you can just create a small kotlin module with what you need.