简体   繁体   中英

MonoTouch NSClassFromString

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.

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. Eg

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). Eg

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 ap/invoke to NSClassFromString would do exactly the same as the ObjectiveC code :-)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM