0

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

  • This is already covered: http://stackoverflow.com/questions/3027120/how-to-make-iphone-app-for-multiple-firmwares/3027213#3027213 – progrmr Jun 27 '10 at 19:21

1 Answers1

2

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.

mohsenr
  • 7,235
  • 3
  • 28
  • 28
  • 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 –  Jun 27 '10 at 18:16
  • you should import iAd as usual (`#import `). 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. – mohsenr Jun 27 '10 at 18:50