Itachi
Itachi

Reputation: 6070

iOS: How to distinguish between iPhone mode and iPad mode?

I'm building an universal iOS app and some config variables have different value between iPhone mode and iPad mode. Please notice that I said the Mode word.

As I know, users could install an iPhone app in iPad from AppStore, so there is something confused to me about the following code:

NSString *deviceType = [UIDevice currentDevice].model;

if([deviceType rangeOfString:@"iPad" options:NSCaseInsensitiveSearch].location != NSNotFound)
{
    fCurrentDeviceIsiPad = YES;
    // ...
}
else // iPhone? iPod? 
{
    fCurrentDeviceIsiPad = NO;
    // ...
}

If user install & run the iPhone version app in iPad, the fCurrentDeviceIsiPad will be YES (I tested the result with debugging it in my iPad for iPhone version, certainly, I'd changed the deployment device as "iPhone" only first).

So, my question is how could I distinguish the app mode without taking the device type as consideration?

Someone posted something about the OS macros, I've tried them in my app.

#if TARGET_OS_IPHONE
  // iPhone specific code
  #error A
#elif TARGET_OS_IPAD
  // iPad specific code
  #error B
#else
  // Unknown target
  #error C
#endif

This didn't hit my point, either. I switched the debug device to iPhone simulator and iPad simulator, all the compile errors are hit on error A. Did I misunderstand the meaning of these macros?

One more question, this will be the first time I submit an iOS to AppStore and it's an universal app. For the submit process, is it standalone process for uploading iPhone version binary and iPad version binary? You may get my new point: if the answer is yes, it's ok for me to build two versions manually with my custom-macros.

Thanks!

UPDATE: Problem solved!

The code [UIDevice currentDevice].model is NOT the same with the UI_USER_INTERFACE_IDIOM(), the former one always returns the real device type, the latter one is the current running app mode/version.

Upvotes: 0

Views: 382

Answers (2)

try catch finally
try catch finally

Reputation: 881

Create an NSObject file and name it Common. Delete the .m and delete all the code written in Common.h Import the Common.h file in .pch file Put this is the Common.h file And then you can use it throughout your app.

#define IS_IPHONE (!IS_IPAD)
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPhone)
#define IS_IPHONE_5 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )

Thanks.

Upvotes: 1

Divyang Shah
Divyang Shah

Reputation: 1668

BOOL isIpad() {
    return UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad;
}

Upvotes: 5

Related Questions