Reputation: 55
I created project in Xcode 11 beta 2, with newly introduced cross-platform feature, which targets both iOS and macOS. I have troubles identifying whether the app is run on one platform or another.
Let's say I added enum, which helps to identify the platform
enum Platform {
case iOS
case macOS
case unknown
}
Now if I try the following code I always obtain that the platform is iOS no matter what environment I run.
#if canImport(UIKit)
let platform: Platform = .iOS
#elseif canImport(AppKit)
let platform: Platform = .macOS
#else
let platform: Platform = .unknown
#endif
I also tried different convention #if os(iOS) || os(watchOS) || os(tvOS)
, but since the Swift version I use is 5.0 it should not matter.
The only solution that comes into my mind is to add different
Ok I just tried setting it to OTHER_SWIFT_FLAGS
in build settings depending on the architecture I use.OTHER_SWIFT_FLAGS[sdk=macosx*] = "-D" "MACOS"
and it is still treated like iOS.
I just don't get why the code proposed above does not work. I'm currently preparing more space to install beta 3 (128gb storage ftw) in order to check if the output is similar.
Upvotes: 1
Views: 233
Reputation: 6701
you mention using #if os(iOS)
but don't talk about using the macOS
flag. have you tried a solution like this?
enum Platform {
case iOS
case macOS
case unknown
}
var compiledPlatform: Platform {
#if os(iOS)
return .iOS
#elseif os(macOS)
return .macOS
#else
return .unknown
#endif
}
UPDATE:
i haven't had a chance to play with the new cross compiling features, but my guess is that you are compiling the code exactly once for both Mac and iOS.
since the code is only compiled once, the preprocessor commands are also only ran once. this makes it so it will always default to either iOS or Mac and omit the code for the other. in your case it seems to be iOS.
i would recommend removing the preprocessor check and making it a runtime check instead. there are definitely cleaner ways, but something along the lines of this:
let isMac = NSClassFromString("NSView") != nil
Upvotes: 2
Reputation: 55
While wandering through Build Settings I found that cross-platform project has SUPPORTED_PLATFORMS = iOS
, thus I might not be able to find the solution to my problem.
This indicates it is somehow interpreted differently.
Upvotes: 1