How to test for the existance of a constant when creating a iPhone universal binary

て烟熏妆下的殇ゞ 提交于 2019-12-24 04:55:13

问题


I am trying to create a universal binary that supports multitasking on the iPhone 4 and can still run on the iPad.

I know how to avoid compile errors for different versions of the iPhone IOS by checking if a class exists by using NSClassFromString and "respondToSelector", but is there a way to check for the existence of constants like UIBackgroundTaskInvalid?

I of course can use #IFDEF, but I want to avoid this.


回答1:


You do it as follows:

if (NULL != &UIBackgroundTaskInvalid) {
   //do multitasking stuff here
} else {
   // don't do multitasking stuff here.
}

Basically you want to verify that the address of that variable exists.

Update: To be clear, you can't really use an #ifdef for this since you will build with a version of the SDK that contains the symbols.




回答2:


It's probably sufficient to test for the existence of a method that you know is associated with the constant.




回答3:


The preferred method to check for multitasking iOS is to see if UIDevice responds to isMultitaskingSupported, like this:

//----------------------------------------------------------------------
// returns YES if multitasking is available (iOS 4.0 or >)
//----------------------------------------------------------------------
BOOL hasMultitasking() 
{
    UIDevice* device = [UIDevice currentDevice];
    if ([device respondsToSelector:@selector(isMultitaskingSupported)]) {
        return [device isMultitaskingSupported];
    }
    return NO;
}

If multitasking is supported then you can use those multitasking related constants, which you should have weak linked to.



来源:https://stackoverflow.com/questions/3362544/how-to-test-for-the-existance-of-a-constant-when-creating-a-iphone-universal-bin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!