How can I add platform specific code to the new cross-platform swiftUI app

XCode 12 offers a new cross-platform swiftUI app - awesome
I would like to add platform specific code conditionals

e.g.

Code Block
if(isMacos())
doLoginMacos()
else
doLoginIOS()

or


Code Block
var body = macos ? ComplexNonMobileContentView() : Text("blabla mobile")


or

Code Block
func onLoad {
if(isMacos) addStatusbarItem
}
//note i dont even know if the new App Protocol has a onLoad ;) Happy to add fully separate AppDelegates and all too

Replies

The platform check syntax for Swift is

Code Block Swift
#if os(macOS)
...
#elseif os(iOS)
...
#endif


That gives coarse grained control. If you need to check availability of specific APIs or specific OS versions you’ll need to use @available.
  • does #elseif os(iOS) include iPadOS?

Add a Comment
This is how you would do a check to see what platform the app is running on

Code Block
#if os(iOS) os(watchOS)
doLoginiOS()
#elseif os(macOS)
DoLogInMacOS()
#else
println("OMG, it's that mythical new Apple product!!!")
#endif