views:

625

answers:

2

Can I add the UIBannerViewDelegate protocol to my UIViewController subclass while remaining compatible with pre-iOS 4 devices?

This is NOT a duplicate, the question is specifically related to the delegate protocol.

A: 

Check the other answer for best results

GSchv
come on it was a great answer please check it at least
GSchv
Sometimes you learn more by answering your own question. Either way, you are responsible for marking your chosen answer (even if it was your own).
iWasRobbed
Actually, this answer is wrong. The compiler directives will only come into play at compile-time, not runtime, so if you build this application for 4.0 (to support iAds in compatible devices) and target 3.0, the symbols will still be in your final application.
Brad Larson
and? they'll be in the final app but they won't qualify at runtime? and it would still work pretty well. please clarify
GSchv
@GSchv - The above code only will change what is built into your application, not what actually gets executed. If you build for iOS 4.0 (which you will need to in order to get iAd support), anything between the compiler directives will still run, no matter the OS its running on. You need to use runtime checks like NSClassFromString or -respondsToSelector: to enable or disable features at runtime. In this case, it doesn't matter, because a 4.0 protocol will not cause an error when run on 3.0 (see my answer for more).
Brad Larson
@Brad Larson - are you saying that my answer is not wrong, but that it makes no difference at all inserting that extra code. Or are you saying my answer is wrong and I should remove it from my code (I have already submitted the app - I wanna know if you think it'll work anyway, or that, NO it's wrong). Because I'm under the impression that it will work alright.
GSchv
@GSchv - Sorry, wrong might be a little strong in this case. The extra code is just unneccessary. It won't lead to crashing or rejection, it just won't make a difference in the end product and isn't what you want to do if you need to adjust to a particular OS running on the user's device. For that, see progrmr's link in the comments above.
Brad Larson
+3  A: 

If you weak-link the iAd framework, you will encounter no issues with a controller class that conforms to the ADBannerViewDelegate protocol. You will, of course, need to add the ADBannerView programmatically (if it exists on the running OS) or otherwise load a different Nib file for 3.x vs. 4.x.

Even though there is no ADBannerViewDelegate protocol in iPhone OS 3.x, I just tested and no errors are thrown for missing symbols if a class that conforms to it is loaded in that older OS.

Brad Larson
Yeah, it works this way. You just need to check: "if (NSClassFromString(@"ADBannerView"))". See more code details here: http://iphone-dev-tips.alterplay.com/2010/07/what-ive-already-used-more-than-once.html
slatvick