Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

API to determine whether running on iPhone or iPad [duplicate]

Is there an API for checking at runtime whether you are running on an iPhone or an iPad?

One way I can think of would be to use:

[[UIDevice currentDevice] model]; 

And detect the existence of the string @"iPad" - which seems a bit fragile.

In the 3.2 SDK, I see that UIDevice also has a property which is really what I'm looking for, but doesn't work for pre-3.2 (obviously):

[[UIDevice currentDevice] userInterfaceIdiom];  

Are there other ways than checking for the existence of @"iPad" for a universal app?

like image 878
Eric Avatar asked May 21 '10 18:05

Eric


People also ask

What is APIs in iOS?

An API, or application programming interface, is used to pass data back and forth between software apps in a formalized way. Many services offer public APIs that allow anyone to send and receive content from the service.

How do I know if JavaScript is on my iPhone?

Here's how you can check whether someone on your website or app is using a mobile iOS device in JavaScript: const ios = () => { if (typeof window === `undefined` || typeof navigator === `undefined`) return false; return /iPhone|iPad|iPod/i. test(navigator.


2 Answers

Checkout UI_USER_INTERFACE_IDIOM.

Returns the interface idiom supported by the current device.

Return Value
UIUserInterfaceIdiomPhone if the device is an iPhone or iPod touch or UIUserInterfaceIdiomPad if the device is an iPad.

UIUserInterfaceIdiom

The type of interface that should be used on the current device

typedef enum {    UIUserInterfaceIdiomPhone,    UIUserInterfaceIdiomPad, } UIUserInterfaceIdiom; 
like image 109
Andiih Avatar answered Oct 05 '22 06:10

Andiih


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.

like image 28
Dan Rosenstark Avatar answered Oct 05 '22 08:10

Dan Rosenstark