iOS wykrywa, czy użytkownik jest na iPadzie

[1]}mam aplikację, która działa na iPhone i iPod Touch, może działać na Retina iPad i wszystko, ale musi być jedna Regulacja. Muszę wykryć, czy obecne urządzenie to iPad. Jaki kod mogę użyć, aby wykryć, czy użytkownik korzysta z iPada w moim UIViewController, a następnie odpowiednio zmienić coś?

Author: Bartłomiej Semańczyk, 2012-04-16

15 answers

Istnieje kilka sposobów, aby sprawdzić, czy urządzenie jest iPadem. Jest to mój ulubiony sposób, aby sprawdzić, czy urządzenie jest w rzeczywistości iPadem:

if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad )
{
    return YES; /* Device is iPad */
}

The way I use it

#define IDIOM    UI_USER_INTERFACE_IDIOM()
#define IPAD     UIUserInterfaceIdiomPad

if ( IDIOM == IPAD ) {
    /* do something specifically for iPad. */
} else {
    /* do something specifically for iPhone or iPod touch. */
}   

Inne Przykłady

if ( [(NSString*)[UIDevice currentDevice].model hasPrefix:@"iPad"] ) {
    return YES; /* Device is iPad */
}

#define IPAD     (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
if ( IPAD ) 
     return YES;

Aby uzyskać szybkie rozwiązanie, zobacz odpowiedź: https://stackoverflow.com/a/27517536/2057171

 540
Author: WrightsCS,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-02-20 07:24:36

W Swift możesz użyć następujących równań do określenia rodzaju urządzenia w aplikacjach uniwersalnych:

UIDevice.current.userInterfaceIdiom == .phone
// or
UIDevice.current.userInterfaceIdiom == .pad

Użycie byłoby wtedy czymś w rodzaju:

if UIDevice.current.userInterfaceIdiom == .pad {
    // Available Idioms - .pad, .phone, .tv, .carPlay, .unspecified
    // Implement your logic here
}
 114
Author: Dschee,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-02-06 04:25:09

Jest to część UIDevice od iOS 3.2, np.:

[UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad
 33
Author: Richard,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-11-22 13:16:34

Możesz również użyć tego

#define IPAD UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad
...
if (IPAD) {
   // iPad
} else {
   // iPhone / iPod Touch
}
 25
Author: Chilly,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-01-28 13:39:42

UI_USER_INTERFACE_IDIOM () zwraca iPada tylko wtedy, gdy aplikacja jest przeznaczona dla iPada lub Universal. Jeśli jest to aplikacja na iPhone ' a uruchamiana na iPadzie, to nie będzie. więc powinieneś zamiast tego sprawdzić model.

 19
Author: malhal,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-01-27 21:06:09

Bądź ostrożny: jeśli Twoja aplikacja jest kierowana tylko na urządzenie iPhone, iPad działający w trybie zgodnym z iphone ' em zwróci false dla poniższej instrukcji:

#define IPAD     UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad

Właściwym sposobem wykrycia fizycznego urządzenia iPada jest:

#define IS_IPAD_DEVICE      ([(NSString *)[UIDevice currentDevice].model hasPrefix:@"iPad"])
 14
Author: peak,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-06-12 07:41:40

Odkryłem, że jakieś rozwiązanie nie działa dla mnie w symulatorze w Xcode. Zamiast tego działa:

ObjC

NSString *deviceModel = (NSString*)[UIDevice currentDevice].model;

if ([[deviceModel substringWithRange:NSMakeRange(0, 4)] isEqualToString:@"iPad"]) {
    DebugLog(@"iPad");
} else {
    DebugLog(@"iPhone or iPod Touch");
}

Swift

if UIDevice.current.model.hasPrefix("iPad") {
    print("iPad")
} else {
    print("iPhone or iPod Touch")
}

Również w "innych przykładach" w Xcode model urządzenia wraca jako "symulator iPada", więc powyższy tweak powinien to uporządkować.

 10
Author: Andy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-02-06 04:10:26

Wiele Sposobów, aby to zrobić w Swift :

Sprawdzamy poniższy model (tutaj możemy tylko przeszukiwać wielkość liter):

class func isUserUsingAnIpad() -> Bool {
    let deviceModel = UIDevice.currentDevice().model
    let result: Bool = NSString(string: deviceModel).containsString("iPad")
    return result
}

Sprawdzamy poniższy model (tutaj możemy przeszukiwać wielkość liter / wielkość liter):

    class func isUserUsingAnIpad() -> Bool {
        let deviceModel = UIDevice.currentDevice().model
        let deviceModelNumberOfCharacters: Int = count(deviceModel)
        if deviceModel.rangeOfString("iPad",
                                     options: NSStringCompareOptions.LiteralSearch,
                                     range: Range<String.Index>(start: deviceModel.startIndex,
                                                                end: advance(deviceModel.startIndex, deviceModelNumberOfCharacters)),
                                     locale: nil) != nil {
            return true
        } else {
            return false
        }
   }

UIDevice.currentDevice().userInterfaceIdiom poniżej zwraca tylko iPada, jeśli aplikacja jest na iPada lub Universal. Jeśli jest to aplikacja na iPhone ' a uruchamiana na iPadzie, to nie będzie. więc powinieneś zamiast tego sprawdzić model. :

    class func isUserUsingAnIpad() -> Bool {
        if UIDevice.currentDevice().userInterfaceIdiom == UIUserInterfaceIdiom.Pad {
            return true
        } else {
            return false
        }
   }

Poniższy fragment nie kompiluje się, jeśli klasa nie dziedziczenie UIViewController, w przeciwnym razie działa dobrze. Niezależnie UI_USER_INTERFACE_IDIOM() zwraca iPada tylko wtedy, gdy aplikacja jest na iPada lub Universal. Jeśli jest to aplikacja na iPhone ' a uruchamiana na iPadzie, to nie będzie. więc powinieneś zamiast tego sprawdzić model. :

class func isUserUsingAnIpad() -> Bool {
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.Pad) {
        return true
    } else {
        return false
    }
}
 8
Author: King-Wizard,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-06-02 18:13:26

*

W swift 3.0

*

 if UIDevice.current.userInterfaceIdiom == .pad {
        //pad
    } else if UIDevice.current.userInterfaceIdiom == .phone {
        //phone
    } else if UIDevice.current.userInterfaceIdiom == .tv {
        //tv
    } else if UIDevice.current.userInterfaceIdiom == .carPlay {
        //CarDisplay
    } else {
        //unspecified
    }
 6
Author: Ashok R,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-10-09 03:22:08

Możesz sprawdzić rangeOfString, aby zobaczyć słowo iPad istnieje w ten sposób.

NSString *deviceModel = (NSString*)[UIDevice currentDevice].model;

if ([deviceModel rangeOfString:@"iPad"].location != NSNotFound)  {
NSLog(@"I am an iPad");
} else {
NSLog(@"I am not an iPad");
}
 3
Author: LearningGuy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-09-03 04:22:12

Yet another Swifty way:

//MARK: -  Device Check
let iPad = UIUserInterfaceIdiom.Pad
let iPhone = UIUserInterfaceIdiom.Phone
@available(iOS 9.0, *) /* AppleTV check is iOS9+ */
let TV = UIUserInterfaceIdiom.TV

extension UIDevice {
    static var type: UIUserInterfaceIdiom 
        { return UIDevice.currentDevice().userInterfaceIdiom }
}

Użycie:

if UIDevice.type == iPhone {
    //it's an iPhone!
}

if UIDevice.type == iPad {
    //it's an iPad!
}

if UIDevice.type == TV {
    //it's an TV!
}
 2
Author: Aviel Gross,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-07-06 05:49:39

Wiele odpowiedzi jest dobrych, ale używam tego w swift 4

  1. Utwórz Stałą

    struct App {
        static let isRunningOnIpad = UIDevice.current.userInterfaceIdiom == .pad ? true : false
    }
    
  2. Użyj w ten sposób

    if App.isRunningOnIpad {
        return load(from: .main, identifier: identifier)
    } else {
        return load(from: .ipad, identifier: identifier)
    }
    

Edytuj: zgodnie z sugestią Cœur po prostu utwórz rozszerzenie na UIDevice

extension UIDevice {
static let isRunningOnIpad = UIDevice.current.userInterfaceIdiom == .pad ? true : false

}

 2
Author: Rohit Sisodia,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-04-03 10:45:44

W przypadku najnowszych wersji iOS wystarczy dodać UITraitCollection:

extension UITraitCollection {

    var isIpad: Bool {
        return horizontalSizeClass == .regular && verticalSizeClass == .regular
    }
}

A następnie w UIViewController wystarczy sprawdzić:

if traitCollection.isIpad { ... }
 0
Author: Bartłomiej Semańczyk,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-02-06 13:36:23
if(UI_USER_INTERFACE_IDIOM () == UIUserInterfaceIdiom.pad)
 {
            print("This is iPad")
 }else if (UI_USER_INTERFACE_IDIOM () == UIUserInterfaceIdiom.phone)
 {
            print("This is iPhone");
  }
 0
Author: Rajesh Sharma,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-04-27 10:03:47

Dlaczego tak skomplikowane? Tak to robię...

Swift 4:

var iPad : Bool {
    return UIDevice.current.model.contains("iPad")
}

W ten sposób możesz po prostu powiedzieć if iPad {}

 0
Author: The Ruffus,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-09-20 22:05:00