0

I have seen this question where OP asks if there's a way to check which hour format the device is using for iOS. The selected answer also has the solution for Android. However, in my xamarin.forms application, I cannot build or run the app in iOS because I get Java.Interop missing error. I'm writing a simple method to return bool if its using 12 hour format.

public bool GetHourFormat()
{
    bool TwelveHourFormat = true;
    if (Device.RuntimePlatform == "iOS")
    {
        var dateFormatter = new NSDateFormatter();
        dateFormatter.DateStyle = NSDateFormatterStyle.None;
        dateFormatter.TimeStyle = NSDateFormatterStyle.Short;

        var dateString = dateFormatter.ToString(NSDate.Now);
        TwelveHourFormat =
        dateString.Contains(dateFormatter.AMSymbol) ||
        dateString.Contains(dateFormatter.PMSymbol);
    }
    else if (Device.RuntimePlatform == "Android")
    {
        TwelveHourFormat = Android.Text.Format.DateFormat.Is24HourFormat(Android.App.Application.Context);
    }
    return TwelveHourFormat;
}

Is there any general way to get this information without relying on platform? If not, how can I get this information in both platforms?

cptalpdeniz
  • 368
  • 1
  • 11
  • 1
    When using platform-specific calls, try a separate function for each platform. The `Android.*` stuff needs to be in a function that iOS never calls, and vice versa. – Ben Voigt Sep 13 '18 at 02:55
  • 1
    For the reason having separate helper functions is important, read https://stackoverflow.com/q/5233155/103167 – Ben Voigt Sep 13 '18 at 02:56
  • @BenVoigt separating the functions fixed the problem. I'll take a look at the link. Thank you. – cptalpdeniz Sep 13 '18 at 03:03
  • @BenVoigt would you like to write the answer or do you want me to write it? – cptalpdeniz Sep 13 '18 at 03:03
  • Tempted to mark as a duplicate, but even though the cause is the same, none of the platform details are. And it's not really what you asked. I'll write an answer if you ask a question about how to avoid "Interop missing error" when you have both iOS and Android platform-specific code. – Ben Voigt Sep 13 '18 at 03:24

1 Answers1

0

I will use Preprocessor, so only platform specific are compiled depending on the platform (Android / iOS) you are using.

private bool CheckIsTwelveTimeFormat()
{
#if __ANDROID__
    // code in this #if block is only compiled on Android
    return !Android.Text.Format.DateFormat.Is24HourFormat(Android.App.Application.Context);
#elif __IOS__
    // code in this #elif block is only compiled on iOS
    var dateFormatter = new Foundation.NSDateFormatter {
        DateStyle = Foundation.NSDateFormatterStyle.None,
        TimeStyle = Foundation.NSDateFormatterStyle.Short
    };

    var dateString = dateFormatter.ToString(Foundation.NSDate.Now);
    var isTwelveHourFormat =
    dateString.Contains(dateFormatter.AMSymbol) ||
    dateString.Contains(dateFormatter.PMSymbol);
    return isTwelveHourFormat;
#endif
}
Patrick
  • 1,717
  • 7
  • 21
  • 28