views:

82

answers:

3

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.

A: 

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.

Elfred
@Elfred: you win ;-) but it looks like asking for trouble.
mvds
In the "applicationDidEnterBackground" selector that I have to overload for multitasking I get a compile error for the " NSAssert(backgroundTask == UIBackgroundTaskInvalid, nil);" line. Obviously this selector will not get called when running on the iPad, but it will not compile if I want to test it using 3.2. So what is the smart way to do this?
Ken
You always build with the iOS 4 SDK and set the deployment target to 3.2
Elfred
Yes, but how do I test the code in the 3.2 simulator?
Ken
Ah, I see. So for testing set the deployment target to 3.2, but when compiling for distribution set the deployment target to 3.0. Correct?
Ken
If your minimum target is 3.0 you can leave the deployment target as 3.0. If you want to simulate it on the iPad, you can set it as the 3.2 simulator or change the device type once the simulator is open.
Elfred
+1  A: 

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

Brian
Not really because my constant must still appear in the code and will not pass the compiler test.
Ken
Yes it will as long as you compile with the BaseSDK 4.0, all the 4.0 constants will be defined, but you should not use them on < 4.0 devices (weak links will not resolve when running on those devices).
progrmr
+1  A: 

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.

progrmr