2

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 
}
Vakas
  • 6,291
  • 3
  • 35
  • 47
Aravindh Kumar
  • 1,213
  • 11
  • 22
  • Your link does not work. – Martin R Apr 09 '16 at 10:37
  • did you check with var device = UIDevice.currentDevice().model – Nitin Gohel Apr 09 '16 at 10:38
  • Have a look at http://stackoverflow.com/questions/30275403/how-to-detect-iphone-6-6-plus-view-mode-programmatically, should be easy to translate to Swift. – Martin R Apr 09 '16 at 10:38
  • @MartinR the same url i linked i tried of converting to swift but it doesnot work can u help me out to solve this? – Aravindh Kumar Apr 09 '16 at 10:42
  • @NitinGohel `UIDevice.currentDevice().model` it returns iphone i need to detect zoomed or standard mode of iphone – Aravindh Kumar Apr 09 '16 at 10:45
  • http://stackoverflow.com/a/28649478/2303865 – Leo Dabus Apr 09 '16 at 11:50
  • In the commented lines you wrote zoomed and standard. If you really want to know how to detect the phone, other answers are correct. If you want to port an app to iPhone 6 have a look here https://www.raywenderlich.com/89816/porting-app-iphone-6-iphone-6-plus-ios-8-top-10-tips – BHuelse Apr 09 '16 at 16:22

5 Answers5

3

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
}

}
Vakas
  • 6,291
  • 3
  • 35
  • 47
2

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

Community
  • 1
  • 1
kamwysoc
  • 6,709
  • 2
  • 34
  • 48
  • thanks @k8mil i checked for iphone 6splus standard( nativescale==2.60) and for iphone 6splus zoomed( nativescale==2.88) any idea for other device say 6,6s,6plus. – Aravindh Kumar Apr 09 '16 at 11:03
0

I would recommend using an existing, off-the-shelf solution for such generic problems. E.g., 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).

Thomas Köhn
  • 123
  • 7
0

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

owlswipe
  • 19,159
  • 9
  • 37
  • 82
0

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

Aravindh Kumar
  • 1,213
  • 11
  • 22