Skip to content

Instantly share code, notes, and snippets.

@jamesmontemagno
Last active January 5, 2021 14:31
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jamesmontemagno/f206315dbbee74a277a0 to your computer and use it in GitHub Desktop.
Save jamesmontemagno/f206315dbbee74a277a0 to your computer and use it in GitHub Desktop.
Snippet Name: iOS Utils
Platform: Xamarin.iOS
Function: Check if running iOS 8, is phone, is tall, and envoke on UI Thread
Snippet:
public static class Utils
{
public static NSObject Invoker;
/// <summary>
/// Ensures the invoked on main thread.
/// </summary>
/// <param name="action">Action to run on main thread.</param>
public static void EnsureInvokedOnMainThread(Action action)
{
if (NSThread.Current.IsMainThread)
{
action();
return;
}
if (Invoker == null)
Invoker = new NSObject();
Invoker.BeginInvokeOnMainThread(() => action());
}
public static bool IsiOS7
{
get
{
return UIDevice.CurrentDevice.CheckSystemVersion(7, 0);
}
}
public static bool IsiOS8
{
get
{
return UIDevice.CurrentDevice.CheckSystemVersion(8, 0);
}
}
public static bool IsPhone
{
get { return UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Phone; }
}
//Check if taller than iPhone 4
public static bool IsTallPhone
{
get
{
return IsPhone && (UIScreen.MainScreen.Bounds.Height * UIScreen.MainScreen.Scale >= 1136);
}
}
}
Example Use:
//Pop up items
Utils.EnsureInvokedOnMainThread (() => {
var alertView = new UIAlertView ("Title", "Message", null, "OK");
alertView.Show ();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment