robado por perdido descargar buscar bloquear apple ios objective-c swift ipad device

por - iOS detecta si el usuario está en un iPad



ios 12 descargar (16)

¿Por qué tan complicado? Así es como lo hago ...

Swift 4:

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

De esta manera solo puedes decir if iPad {}

Tengo una aplicación que se ejecuta en el iPhone y el iPod Touch, se puede ejecutar en el iPad Retina y todo, pero debe haber un ajuste. Necesito detectar si el dispositivo actual es un iPad. ¿Qué código puedo usar para detectar si el usuario está usando un iPad en mi UIViewController y luego cambiar algo en consecuencia?


*

En 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 }


En Swift 4.2 y Xcode 10

if UIDevice().userInterfaceIdiom == .phone { //This is iPhone } else if UIDevice().userInterfaceIdiom == .pad { //This is iPad } else if UIDevice().userInterfaceIdiom == .tv { //This is Apple TV }

Si quieres detectar un dispositivo específico.

let screenHeight = UIScreen.main.bounds.size.height if UIDevice().userInterfaceIdiom == .phone { if (screenHeight >= 667) { print("iPhone 6 and later") } else if (screenHeight == 568) { print("SE, 5C, 5S") } else if(screenHeight<=480){ print("4S") } } else if UIDevice().userInterfaceIdiom == .pad { //This is iPad }


En Swift puede usar las siguientes ecualizaciones para determinar el tipo de dispositivo en las aplicaciones de Universal:

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

El uso sería entonces algo como:

if UIDevice.current.userInterfaceIdiom == .pad { // Available Idioms - .pad, .phone, .tv, .carPlay, .unspecified // Implement your logic here }


Encontré que alguna solución no me funcionó en el simulador dentro de Xcode. En su lugar, esto funciona:

ObjC

NSString *deviceModel = (NSString*)[UIDevice currentDevice].model; if ([[deviceModel substringWithRange:NSMakeRange(0, 4)] isEqualToString:@"iPad"]) { DebugLog(@"iPad"); } else { DebugLog(@"iPhone or iPod Touch"); }

Rápido

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

También en los "Otros ejemplos" en Xcode, el modelo del dispositivo regresa como "Simulador de iPad", por lo que el ajuste anterior debería solucionarlo.


Esto es parte de UIDevice a partir de iOS 3.2, por ejemplo:

[UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad


Hay bastantes maneras de verificar si un dispositivo es un iPad. Esta es mi forma favorita de comprobar si el dispositivo es en realidad un iPad:

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

La forma en que lo uso

#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. */ }

Otros ejemplos

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

Para una solución Swift, vea esta respuesta: https://.com/a/27517536/2057171


Muchas formas de hacer eso en Swift :

Verificamos el modelo a continuación (solo podemos hacer una búsqueda de mayúsculas y minúsculas aquí):

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

Verificamos el modelo a continuación (podemos hacer una búsqueda que distingue entre mayúsculas y minúsculas aquí):

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 continuación solo devuelve iPad si la aplicación es para iPad o Universal. Si es una aplicación de iPhone que se ejecuta en un iPad, entonces no lo hará. Así que en su lugar deberías revisar el modelo. :

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

Este fragmento de código a continuación no se compila si la clase no hereda de un UIViewController , de lo contrario funciona bien. Independientemente, UI_USER_INTERFACE_IDIOM() solo devuelve iPad si la aplicación es para iPad o Universal. Si es una aplicación de iPhone que se ejecuta en un iPad, entonces no lo hará. Así que en su lugar deberías revisar el modelo. :

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


Otra manera más Swifty:

//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 } }

Uso:

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


Para las últimas versiones de iOS, simplemente agregue UITraitCollection :

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

y luego dentro de UIViewController solo verifica:

if traitCollection.isIpad { ... }


Puedes verificar el rangeOfString para ver si la palabra iPad existe así.

NSString *deviceModel = (NSString*)[UIDevice currentDevice].model; if ([deviceModel rangeOfString:@"iPad"].location != NSNotFound) { NSLog(@"I am an iPad"); } else { NSLog(@"I am not an iPad"); }


También puedes usar este

#define IPAD UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ... if (IPAD) { // iPad } else { // iPhone / iPod Touch }


Tenga cuidado: si su aplicación está orientada solo al dispositivo iPhone, el iPad que se ejecuta con el modo compatible con iPhone devolverá el valor falso para la siguiente declaración:

#define IPAD UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad

La forma correcta de detectar el dispositivo físico iPad es:

#define IS_IPAD_DEVICE ([(NSString *)[UIDevice currentDevice].model hasPrefix:@"iPad"])


UI_USER_INTERFACE_IDIOM () solo devuelve iPad si la aplicación es para iPad o Universal. Si es una aplicación de iPhone que se ejecuta en un iPad, entonces no lo hará. Así que en su lugar deberías revisar el modelo.


Muchas respuestas son buenas, pero las uso de este modo en swift 4

  1. Crear constante

    struct App { static let isRunningOnIpad = UIDevice.current.userInterfaceIdiom == .pad ? true : false }

  2. Usar asi

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

Edición: según lo sugerido, simplemente cree una extensión en UIDevice

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

}


if(UI_USER_INTERFACE_IDIOM () == UIUserInterfaceIdiom.pad) { print("This is iPad") }else if (UI_USER_INTERFACE_IDIOM () == UIUserInterfaceIdiom.phone) { print("This is iPhone"); }