1

I am creating UniversalApp programmatically. in my app i have 2 constant class's and on the basis of device i want to import my constant class. but it always open "Constants_iPad" class. even condition is true or false.

#ifndef iPad
    #define iPad    (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#endif
#ifndef iPhone
    #define iPhone  (UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPad)
#endif


#ifdef iPad  ------> always excute this condition either True of False.
    #import "Constants_iPad.h" // if device is iPad
#else
    #import "Constants_iPhone.h" // if device is iPhone
#endif

When i print on consol using

 #ifdef iPad
     NSLog(@"iPad = %d",iPad);
 #endif

then it print "0" if condition is wrong and if condition is true it print 1

lnafziger
  • 25,760
  • 8
  • 60
  • 101
  • 4
    The problem is that preprocessor directive `#ifdef` actually not executing your statement but only finds out whether your variable `iPad` is defined. In your code it's always defined so `#ifdef iPad` always true. – tsionyx Jan 28 '13 at 07:53
  • 1
    so is there any methord to solve my problem..if yes then please give me some guide line. –  Jan 29 '13 at 05:36
  • I think it's imporsible. Import statement is something the compiler does at compilation time. "#define iPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)" is something that you can use at runtime, not at compilation time – igrek Aug 15 '14 at 07:33
  • So you cant check a runtime condition and import something depending on true or false. Use if statements everywhere or add some helper methods to handle this check for you – igrek Aug 15 '14 at 07:35

3 Answers3

1

There are a couple of problems with your logic here, but the biggest issue is that you can't determine if the user is running the program on an iPad or an iPhone until the program is actually running on the device (long after it is compiled)!

While this won't actually help you accomplish what you are trying to do, the reason that it is always importing your Constants_iPad.h is because at the top, you define the macro called iPad and then below you say "if iPad is defined, then import this file". Well, it is always defined. You just did it on the second line.

To accomplish what you are trying to do, you need to include both files (make sure that your definitions are unique).

Then, in your implementation file you use something like:

if (iPad) {
    // Use the iPad definitions to do what you want
} else {
    // Use the iPhone definitions
}
lnafziger
  • 25,760
  • 8
  • 60
  • 101
  • 1
    When i print on consol using #ifdef iPad NSLog(@"iPad = %d",iPad); #endif then it print "0" if condition is wrong and if condition is true it print 1 –  Jan 28 '13 at 07:48
  • 1
    As it should. It is telling you whether or not iPad is `defined`, not whether or not it `is` an iPad. – lnafziger Jan 28 '13 at 07:51
  • 1
    i want to import class's and on ur way this is not possible for importe class's. –  Jan 29 '13 at 05:35
  • 1
    Your classes need to support both. Use the above method within the class in order to determine how to respond if it is specific to one device or another. Most of your code should apply to both devices and you check for the appropriate device where it differs. – lnafziger Jan 29 '13 at 06:06
1

hava you tried #ifdef TARGET_IPHONE or #ifdef TARGET_IPAD instead of #ifndef iPhone ? I am not sure about the macro #ifndef iPhone? But apart from that i do not think that it is possible to determine the target during precompile time for importing classes.

Ilker Baltaci
  • 11,644
  • 6
  • 63
  • 79
  • 1
    He said that he wants a universal app so it should support both and not have different code with different targets.... – lnafziger Jan 28 '13 at 07:48
  • 1
    "lnafziger" is right, yes i want to do my app support both and not have different code with different targets... –  Jan 29 '13 at 05:33
  • #ifdef TARGET_IPAD is showing "symbol not found".. this is not working can you please show it in detail.. – Ayaz Feb 06 '13 at 05:21
0

EDIT: you must use #if instead of #ifdef. As this said, it's a common mistake.

#if TARGET_OS_IPAD
    #import "Constants_iPad.h" // if device is iPad
#else
    #import "Constants_iPhone.h" // if device is iPhone
#endif

And other macroses here.

Community
  • 1
  • 1
tsionyx
  • 1,629
  • 1
  • 17
  • 34
  • 1
    T_12 :- thnx for u r replay i try this but it's not working for me when i selecte target any target ipad or iphone it always true 1 condition and when it use TARGET_OS_IPAD then it show me error. please help me how to solve this problem. –  Jan 30 '13 at 05:32
  • #ifdef TARGET_IPAD is showing "symbol not found".. this is not working can you please show it in detail.. – Ayaz Feb 06 '13 at 05:21