Just for my reference:
@property (nonatomic, readonly) BOOL isPhone;
-(BOOL)isPhone {
return (UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPhone);
}
or use a #define
#define IS_PHONE (UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPhone)
However, if you're using isPhone
all over your code, that's generally bad practice. Use the factory pattern and polymorphism to keep your if
statements contained, so you get objects created for phone or for iPad and then work with those.
Added
I'm using this solution all over my code now. It adds a standard factory pattern into the alloc.
#define ALLOC_PER_DEVICE() id retVal = nil; \
NSString *className = NSStringFromClass(self);\
if (IS_PHONE && ![className hasSuffix:@"Phone"]) {\
className = [NSString stringWithFormat:@"%@Phone", className];\
Class newClass = NSClassFromString(className);\
retVal = [newClass alloc];\
}\
if (!retVal)\
retVal = [super alloc];\
assert(retVal != nil);\
return retVal\
Then my allocs look like this:
+alloc { ALLOC_PER_DEVICE(); }
And I add a subclass called TheClassPhone
for the phone version.
Note: Since there's no multiple inheritance in Objective-C, using inheritance to solve your problems is a bit overrated (i.e., it doesn't work if you have subclasses of subclasses). Nothing like a good if
when you need it.