开发者

conditionally import framework

开发者 https://www.devze.com 2023-01-05 02:03 出处:网络
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.

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 AdV开发者_JAVA技巧iew* 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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消