views:

38

answers:

2

Hi guys, well to begin with I'm sure this is a simple question.

I am developing an iPhone app with the iAd Framework, which only runs for iOS 4.0 or higher.

Still, I wanna choose a iPhone OS 3.0 deployment target, which causes everything to crash.

  • How do I conditionally include the iAd framework? ...I mean, it would be something like: ...if([[UIDevice currentDevice] systemVersion]>=4.0]) #import

Obviously this won't work because I don't know the correct syntax. Also:

  • How do I conditionally declare an AdView* variable?
  • How do I conditionally handle this AdView* variable in my implementation file.

If you guys could help me, I will be very well impressed.

Thanks

+1  A: 

You don't need to change your include, you need to make the iAd (or any other new framework) linked weakly:

In your target, find iAd in the linked frameworks and change its "Role" from "Required" to "Weak".

To handle the variable conditionally, use NSClassFromString function, like this:

Class AdClass = NSClassFromString(@"ADBannerView");
if(AdClass) {//if the class exists
    ADBannerView* myAd = [[AdClass alloc] initWithFrame:CGRectZero];
    // do something with the ad
}

If OS is older than iOS 4.0, AdClass will be nil and the code won't execute. Note that using ADBannerView* as the type of the variable shouldn't cause any problems, as it's just a hint for a compiler and is the same as id after compilation.

Mo
I have tried changing to weak already, but it doesn't seem to work - it keeps saying no such file or directory.What can be wrong? (I went all the way from Target, Get info, and I set the type to weak instead of required).Also, I need to handle my AdBannerView in many methods (bringing it to the front mostly). Where should I declare the snippet you sent? At implementation? ViewDidLoad obviously won't work, so where? (sorry about my basic skills here).Thanks
GSchv
you should import iAd as usual (`#import <iAd/iAd.h>`). You can use the above code anywhere you want to use `ADBannerView` (including `viewDidLoad` if you want). After the first line, `AdClass` is exactly the same as `ADBannerView` class, or nil if not supported, the rest is the same as using the class in normal way.
Mo
A: 

Great - what should I import? #import? Cause this would crash.

Thanks

GSchv
This should be a comment on the appropriate answer, not an answer by itself.
Brad Larson