How to check if a specific method / class / API exists in Xamarin iOS?

In iOS 6 and 7 Compatibility Tips, it is recommended to check specific API like this

public static bool IsiOS7 {
  get { return UIDevice.CurrentDevice.CheckSystemVersion (7, 0); }
}

if(Util.IsiOS7) {
  //Call iOS 7 API
} else {
  //Call iOS 6.1 or earlier API
}

      

The same should be for iOS 8. Now I read that you should check if any method or class exists instead of version checking. Here RespondsToSelector

comes in. It should be something like this

if (RespondsToSelector(new Selector("edgesForExtendedLayout")))
       EdgesForExtendedLayout = UIRectEdge.None;

      

But how would you check if a whole class exists, for example UIAlertController

? Objective-C doesn't NSClassFromString

. I have also seen something like this in Objective-C

if ([UIAlertController class]){
    // ...
}

      

Would something like this do the trick?

Type type=Type.GetType("UIAlertController");
if(type!=null)
{
    // iOS 8 API
}else{
    // iOS 7 or lower
}

      

How can I do this in C #? Or what other ways are there to check UIAlertController

, UISearchController

... etc.

+3


source to share


1 answer


Do it:



using MonoTouch.ObjCRuntime;
bool hasUIAlertController = Class.GetHandle ("UIAlertController") != IntPtr.Zero;
if (hasUIAlertController) {
    // iOS 8
} else {
    // iOS 7 or lower
}

      

+7


source







All Articles