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

雨燕双飞 提交于 2019-11-26 09:55:52

问题


This question already has an answer here:

  • iOS detect if user is on an iPad 16 answers

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?


回答1:


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;



回答2:


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.




回答3:


Use NSClassFromString and an iPad-specific class. Read more here:

http://developer.apple.com/iphone/library/documentation/General/Conceptual/iPadProgrammingGuide/StartingYourProject/StartingYourProject.html#//apple_ref/doc/uid/TP40009370-CH9-SW3




回答4:


  1. Check for the presence of the userInterfaceIdiom property, usings respondsToSelector:. If it doesn't exist, we are on a pre-3.2 device, thus not an iPad.
  2. If userInterfaceIdiom exists, use it.

Edit: ... which is obviously exactly what the UI_USER_INTERFACE_IDIOM() macro does, so use that instead. :)




回答5:


You can check if you run the app on iPhone or iPad by using the following code:

- (NSString *)deviceModel
{
    struct utsname systemInfo;
    uname(&systemInfo);
    return [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
}

- (NSString *) platformString
{
    NSString *platform = [self deviceModel];
    if ([platform isEqualToString:@"iPhone1,1"])    return @"iPhone_2G";
    else if ([platform isEqualToString:@"iPhone1,2"])    return @"iPhone_3G";
    else if ([platform isEqualToString:@"iPhone2,1"])    return @"iPhone_3GS";
    else if ([platform isEqualToString:@"iPhone3,1"])    return @"iPhone_4";
    else if ([platform isEqualToString:@"iPhone3,3"])    return @"Verizon_iPhone_4";
    else if ([platform isEqualToString:@"iPhone4,1"])    return @"iPhone_4S";
    else if ([platform isEqualToString:@"iPhone5,1"])    return @"iPhone_5";
    else if ([platform isEqualToString:@"iPhone5,2"])    return @"iPhone_5";
    else if ([platform isEqualToString:@"iPod1,1"])      return @"iPod_Touch 1G";
    else if ([platform isEqualToString:@"iPod2,1"])      return @"iPod_Touch 2G";
    else if ([platform isEqualToString:@"iPod3,1"])      return @"iPod_Touch 3G";
    else if ([platform isEqualToString:@"iPod4,1"])      return @"iPod_Touch 4G";
    else if ([platform isEqualToString:@"iPad1,1"])           return @"iPad_1G";
    else if ([platform isEqualToString:@"iPad2,1"])      return @"iPad_2(WiFi)";
    else if ([platform isEqualToString:@"iPad2,2"])      return @"iPad_2(GSM)";
    else if ([platform isEqualToString:@"iPad2,3"])      return @"iPad_2(CDMA)";
    else if ([platform isEqualToString:@"iPad3,1"])      return @"iPad_3";
    else if ([platform isEqualToString:@"iPad3,2"])      return @"iPad_3(GSM/CDMA)";
    else if ([platform isEqualToString:@"iPad3,3"])      return @"iPad_3(GSM)";
    else if ([platform isEqualToString:@"iPad3,4"])      return @"iPad_3(GSM)";
    else if ([platform isEqualToString:@"iPad2,5"])      return @"iPad_mini_1G";
    else if ([platform isEqualToString:@"i386"])         return @"Simulator";
    else if ([platform isEqualToString:@"x86_64"])       return @"Simulator";
    return platform;
}


来源:https://stackoverflow.com/questions/2884391/api-to-determine-whether-running-on-iphone-or-ipad

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!