简体   繁体   中英

How to detect iPhone 6 or iPhone 6 Plus view size mode programmatically

I want to detect iOS device screen size and load different storyboards based on that.Any idea how to achieve this in Swift.

is there any default properties like

if #available(iOS 9.0, *) {
 // load zoomed storyboard 
}else{
 // load standard storyboard 
}

Try the following. You can easily check which iDevice is being used. You can even tell if the app is being run on simulator.

private let DeviceList = [
/* iPod 5 */          "iPod5,1": "iPod Touch 5",
/* iPhone 4 */        "iPhone3,1":  "iPhone 4", "iPhone3,2": "iPhone 4", "iPhone3,3": "iPhone 4",
/* iPhone 4S */       "iPhone4,1": "iPhone 4S",
/* iPhone 5 */        "iPhone5,1": "iPhone 5", "iPhone5,2": "iPhone 5",
/* iPhone 5C */       "iPhone5,3": "iPhone 5C", "iPhone5,4": "iPhone 5C",
/* iPhone 5S */       "iPhone6,1": "iPhone 5S", "iPhone6,2": "iPhone 5S",
/* iPhone 6 */        "iPhone7,2": "iPhone 6",
/* iPhone 6 Plus */   "iPhone7,1": "iPhone 6 Plus",
/* iPhone 6S */       "iPhone8,1": "iPhone 6S",
/* iPhone 6S Plus */  "iPhone8,2": "iPhone 6S Plus",
/* iPad 2 */          "iPad2,1": "iPad 2", "iPad2,2": "iPad 2", "iPad2,3": "iPad 2", "iPad2,4": "iPad 2",
/* iPad 3 */          "iPad3,1": "iPad 3", "iPad3,2": "iPad 3", "iPad3,3": "iPad 3",
/* iPad 4 */          "iPad3,4": "iPad 4", "iPad3,5": "iPad 4", "iPad3,6": "iPad 4",
/* iPad Air */        "iPad4,1": "iPad Air", "iPad4,2": "iPad Air", "iPad4,3": "iPad Air",
/* iPad Air 2 */      "iPad5,1": "iPad Air 2", "iPad5,3": "iPad Air 2", "iPad5,4": "iPad Air 2",
/* iPad Mini */       "iPad2,5": "iPad Mini", "iPad2,6": "iPad Mini", "iPad2,7": "iPad Mini",
/* iPad Mini 2 */     "iPad4,4": "iPad Mini", "iPad4,5": "iPad Mini", "iPad4,6": "iPad Mini",
/* iPad Mini 3 */     "iPad4,7": "iPad Mini", "iPad4,8": "iPad Mini", "iPad4,9": "iPad Mini",
/* Simulator */       "x86_64": "Simulator", "i386": "Simulator"
]


public extension UIDevice {

static var modelName: String {
    var systemInfo = utsname()
    uname(&systemInfo)

    let machine = systemInfo.machine
    let mirror = Mirror(reflecting: machine)

    var identifier = ""

    for child in mirror.children {
        if let value = child.value as? Int8 where value != 0 {
            identifier.append(UnicodeScalar(UInt8(value)))
        }
    }
    return DeviceList[identifier] ?? identifier
}

static var isIphone4: Bool {
    return modelName == "iPhone 5" || modelName == "iPhone 5C" || modelName == "iPhone 5S" || UIDevice.isSimulatorIPhone4
}

static var isIphone5: Bool {
    return modelName == "iPhone 4S" || modelName == "iPhone 4" || UIDevice.isSimulatorIPhone5
}

static var isIphone6: Bool {
    return modelName == "iPhone 6" || UIDevice.isSimulatorIPhone6
}
static var isIphone6Plus: Bool {
    return modelName == "iPhone 6 Plus" || UIDevice.isSimulatorIPhone6Plus
}
static var isIphone6S: Bool {
    return modelName == "iPhone 6S"
}
static var isIphone6SPlus: Bool {
    return modelName == "iPhone 6S Plus"
}


static var isIpad: Bool {
    if (UIDevice.currentDevice().model.rangeOfString("iPad") != nil) {
        return true
    }
    return false
}

static var isIphone: Bool {
    return !self.isIpad
}

/// Check if current device is iPhone4S (and earlier) relying on screen heigth
static var isSimulatorIPhone4: Bool {
    return UIDevice.isSimulatorWithScreenHeigth(480)
}

/// Check if current device is iPhone5 relying on screen heigth
static var isSimulatorIPhone5: Bool {
    return UIDevice.isSimulatorWithScreenHeigth(568)
}

/// Check if current device is iPhone6 relying on screen heigth
static var isSimulatorIPhone6: Bool {
    return UIDevice.isSimulatorWithScreenHeigth(667)
}

/// Check if current device is iPhone6 Plus relying on screen heigth
static var isSimulatorIPhone6Plus: Bool {
    return UIDevice.isSimulatorWithScreenHeigth(736)
}

private static func isSimulatorWithScreenHeigth(heigth: CGFloat) -> Bool {
    let screenSize: CGRect = UIScreen.mainScreen().bounds
    return modelName == "Simulator" && screenSize.height == heigth
}

}

You can check nativeScale if it returns 3.0 is iPhone 6s otherwise return 2.0

if UIScreen.mainScreen().nativeScale == 3.0 {
   //iphone 6 plus
} else {
   // iphone 6
}

As apple documentation says :

The native scale factor for the physical screen. (read-only)

Available in iOS 8.0 and later.

UPDATE Try something like this :

let IS_OS_8_OR_LATER = Float(UIDevice.currentDevice().systemVersion) >= 8.0
let IS_IPHONE = UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.Phone
let IS_STANDARD_IPHONE_6 = (IS_IPHONE && UIScreen.mainScreen().bounds.size.height == 667.0 && IS_OS_8_OR_LATER && UIScreen.mainScreen().nativeScale == UIScreen.mainScreen().scale)
let IS_ZOOMED_IPHONE_6 = (IS_IPHONE && UIScreen.mainScreen().bounds.size.height == 568.0 && IS_OS_8_OR_LATER && UIScreen.mainScreen().nativeScale > UIScreen.mainScreen().scale)
let IS_STANDARD_IPHONE_6_PLUS = (IS_IPHONE && UIScreen.mainScreen().bounds.size.height == 736.0 )
let IS_ZOOMED_IPHONE_6_PLUS = (IS_IPHONE && UIScreen.mainScreen().bounds.size.height == 667.0  && IS_OS_8_OR_LATER && UIScreen.mainScreen().nativeScale < UIScreen.mainScreen().scale)

    if IS_ZOOMED_IPHONE_6_PLUS {
        //do something
    }

Also I think you can use the same values for 6sPLUS and 6PLUS.

Hope it help you

I would recommend using an existing, off-the-shelf solution for such generic problems. Eg, I like https://github.com/erichoracek/UIDevice-Hardware . It is a category on UIDevice and lets you determine a lot of things easily about the current device. (since it is in objective c, you would have to integrate it via the bridging header).

Try UIScreen.mainScreen().currentMode. Experiment with values it returns on different devices, and do what Vakas said.

This works for me tested in iphone 6+ and 6s+

if #available(iOS 8.0, *) {
                print("ios greater than 8")
                if(UIScreen.mainScreen().bounds.size.height == 667.0 && UIScreen.mainScreen().nativeScale < UIScreen.mainScreen().scale){
//                    iphone 6+ and 6s+ are in zoomed
                    print("iphone 6+ and 6s+ zoomed ")
                }else{
                    //                    iphone 6+ and 6s+ are in standard 
                    print("iphone 6+ and 6s+ standard ")
                }
            }else{
                print("ios less than 8")
            }

thanks @k8mil

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM