[英]How to check if a certain method/class/API exists in Xamarin iOS?
In iOS 6 & 7 Compatibility Tips it is recommended to check for a specific API like this 在iOS 6和7兼容性提示中 ,建议检查这样的特定API
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 go for iOS 8. Now I read that you should check if a certain method or class exists instead of version checking. 对于iOS 8也是如此。现在我读到你应该检查某个方法或类是否存在而不是版本检查。 Here
RespondsToSelector
comes in. That should be something like this 这里有
RespondsToSelector
。这应该是这样的
if (RespondsToSelector(new Selector("edgesForExtendedLayout")))
EdgesForExtendedLayout = UIRectEdge.None;
But how would one check if a whole class exists like UIAlertController
? 但是如何检查整个类是否像
UIAlertController
一样存在? There is no NSClassFromString
like in Objective-C. Objective-C中没有
NSClassFromString
。 I also saw something like this in Objective-C 我在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 would one do that in C#? 如何在C#中做到这一点? Or which other ways exists to check for
UIAlertController
, UISearchController
, ... and so on. 或者存在哪些其他方法来检查
UIAlertController
, UISearchController
,...等等。
Do this: 做这个:
using MonoTouch.ObjCRuntime;
bool hasUIAlertController = Class.GetHandle ("UIAlertController") != IntPtr.Zero;
if (hasUIAlertController) {
// iOS 8
} else {
// iOS 7 or lower
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.