How to know the current OS / platform of the executing code (Android / iOS)

后端 未结 3 1869
南方客
南方客 2021-01-01 09:51

Using Xamarin.Android and Xamarin.iOS, I need to now the current OS in a shared code section. Be it an enum, an int or a string, it doesn\'t matter.

I tried this:

3条回答
  •  甜味超标
    2021-01-01 10:48

    Using reflection, try to retrieve the value of the Monotouch.Version property (and the equivalent for MfA: Android.OS.Build.VERSION). One of the calls will fail, the other should succeed; that's why you have to use reflection. That's for a real runtime check.

    But as your app is compiled twice, you can fix that value at compile time.

    #if MONOTOUCH
        var platform = "iOS"
    #else
        var platform = "Android"
    #endif
    

提交回复
热议问题