Is it safe to check for UI_USER_INTERFACE_IDIOM() to determine if it's an iPhone or iPad?

好久不见. 提交于 2019-12-07 07:07:15

问题


I've found this code, here:

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        str = [NSString stringWithString:@"Running as an iPad application"];
    } else {
        str = [NSString stringWithString:
                  @"Running as an iPhone/iPod touch application"];
    }

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Platform"
                                                    message:str
                                                   delegate:nil
                                          cancelButtonTitle:@"OK" 
                                          otherButtonTitles:nil];
    [alert show];
    [alert release];   

How safe is this check? Does Apple actually recommend doing this? Or can it happen that it won't detect an iPad as iPad, or iPhone as iPhone?


回答1:


It should be safe enough, it's well-documented by Apple.

That is just shorthand for the following code:

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
// etc

It could conceivably fail if you tried to run this on anything less than iOS 3.2 (as it was only introduced then), but this might not be an issue for you.



来源:https://stackoverflow.com/questions/3905603/is-it-safe-to-check-for-ui-user-interface-idiom-to-determine-if-its-an-iphone

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