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ś?
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
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
}
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
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
}
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.
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"])
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ć.
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
}
}
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
}
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");
}
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!
}
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
-
Utwórz Stałą
struct App { static let isRunningOnIpad = UIDevice.current.userInterfaceIdiom == .pad ? true : false }
-
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
}
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 { ... }
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");
}
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 {}
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