簡體   English   中英

如何確定當前設備是iPhone還是iPad?

[英]How do I determine whether the current device is an iPhone or iPad?

在我的通用應用程序中,我需要檢查當前設備是iPad還是iPhone。 我該如何以編程方式執行此操作? 我打算把代碼放在我的viewDidLoad中。

檢查平台上是否有UISplitViewController類可用,如果是,請確保它是使用Apple宏的iPad(注意UIUserInterfaceIdiomPad常量僅在iOS 3.2及更高版本上可用)。

if (NSClassFromString(@"UISplitViewController") != nil && UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        //currentDeviceType = iPad;
    }
    else {
        //currentDeviceType = iPhone;
    }

我在所有應用中使用這個簡單的功能:

#import "isPad.h"
BOOL isPad () {
    static BOOL isPad;
    static BOOL used = NO;  
    if (used)
        return isPad;
    used = YES;
    NSRange range = [[[UIDevice currentDevice] model] rangeOfString:@"iPad"];
    isPad = range.location != NSNotFound;
    return isPad;
}
try this.. this will help you.   

 NSString *deviceType = [UIDevice currentDevice].model;


        if([deviceType isEqualToString:@"iPod touch"]||[deviceType isEqualToString:@"iPhone"]||[deviceType isEqualToString:@"iPad"]){
        }
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
    // Statements
}

else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
   // Statements
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM