How to determine device type from Swift? (OS X or iOS)

后端 未结 5 733
情深已故
情深已故 2020-12-04 16:46

I know Swift is relatively new, but I was wondering if there was a way to determine the device type?

(Like you used to be able to do with a #define)?

5条回答
  •  借酒劲吻你
    2020-12-04 17:03

    This should provide you with every use case:

    #if os(OSX)
        print("macOS")
    #elseif os(watchOS)
        print("watchOS")
    #elseif os(tvOS)
        print("tvOS")
    #elseif os(iOS)
        #if targetEnvironment(macCatalyst)
            print("macOS - Catalyst")
        #else
            print("iOS")
        #endif
    #endif
    

提交回复
热议问题