检查UI_USER_INTERFACE_IDIOM()以确定它是iPhone还是iPad是否安全?

ope*_*rog 4 iphone universal-binary ipad

我在这里找到了这段代码:

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];   
Run Code Online (Sandbox Code Playgroud)

这张支票有多安全?Apple真的建议这样做吗?或者它可能会发生它不会将iPad检测为iPad或iPhone作为iPhone?

Sed*_*ien 7

它应该足够安全,Apple 已经充分证明了这一点.

这只是以下代码的简写:

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
// etc
Run Code Online (Sandbox Code Playgroud)

如果你试图在低于iOS 3.2的任何东西上运行它,可能会失败(因为它只在那时引入),但这对你来说可能不是问题.

  • 你的陈述不正确.`UI_USER_INTERFACE_IDIOM()`实际定义为`([[[UIDevice currentDevice] respondsToSelector:@selector(userInterfaceIdiom)]?[[UIDevice currentDevice] userInterfaceIdiom]:UIUserInterfaceIdiomPhone)`.所以它不会在iOS <3.2上失败,而你的代码会 (2认同)