Reputation: 26495
I'm trying to use code such as the following Objective-C in MonoTouch:
if (NSClassFromString(@"ADBannerView"))
What is the equivalent in C#?
Basically I'm wanting to use iAd, but I need to check for the existance of ADBannerView
and ADInterstitialAd
, because they are not available on all versions of the OS. (And I'd rather do feature checking than iOS version checking)
I think this could be helpful in other situations as well.
Upvotes: 5
Views: 652
Reputation: 43553
A recent MonoTouch will always provide ADBannerView
so you cannot the the C# equivalent of Type.GetType
to query availability.
Normally a version check is the best way to check for features. E.g.
bool available = UIDevice.CurrentDevice.CheckSystemVersion (4, 0);
will return true for any 4.0+ versions of iOS (4.0 being when ADBannerView
was added to iOS).
A possible alternative (might not work in every case) is to create an instance and check it's handle. Since ObjC is message based sending init
will return null (something a .NET constructor can't do). E.g.
bool available = (new ADBannerView ().Handle != IntPtr.Zero);
Note that you probably best surround the above with using
to dispose the view or integrate this inside the normal creation of your ADBannerView
.
UPDATE: of course a p/invoke to NSClassFromString
would do exactly the same as the ObjectiveC code :-)
Upvotes: 2
Reputation: 11955
This person asked the question and answered it. It looks like you need compiler flags:
Check this out: MonoTouch app with iAds runs on simulator, crashes on device
Otherwise you can Check for class in an assembly.
Lastly you can use the same google search I did: monotouch ADBannerView assembly
If this doesn't help, one of the guys at MonoTouch usually check for monotouch tagged questions and are really helpful.
Upvotes: 0