2

My app needs to know which iphone device is running under and base on that make some screen adjustment. I am using the following code to decide which iphone https://github.com/dennisweissmann/Basics/blob/master/Device.swift

but is there a way to find out if is 6S or 6S Plus? So the point is if is not one of the device mentioned in the Device.swift, then doesn't know how to adjust the screen and will display device not supported, but I want the app behaves same as iPhone6 for iPhone6S and same as iPhone6 Plus for iPhone6s Plus.

Thanks

borna
  • 906
  • 3
  • 12
  • 32
  • Best way is to match "Hardware strings" from here https://en.wikipedia.org/wiki/List_of_iOS_devices to `uname` function output. Your library is out-dated – creker Sep 19 '15 at 03:04

4 Answers4

4

You can do this programatically by getting device model:

#import <sys/utsname.h>

NSString* deviceName()
{
    struct utsname systemInfo;
    uname(&systemInfo);

    return [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
}

Resultant Models:

@"i386"      on 32-bit Simulator
@"x86_64"    on 64-bit Simulator
@"iPod1,1"   on iPod Touch
@"iPod2,1"   on iPod Touch Second Generation
@"iPod3,1"   on iPod Touch Third Generation
@"iPod4,1"   on iPod Touch Fourth Generation
@"iPhone1,1" on iPhone
@"iPhone1,2" on iPhone 3G
@"iPhone2,1" on iPhone 3GS
@"iPad1,1"   on iPad
@"iPad2,1"   on iPad 2
@"iPad3,1"   on 3rd Generation iPad
@"iPhone3,1" on iPhone 4 (GSM)
@"iPhone3,3" on iPhone 4 (CDMA/Verizon/Sprint)
@"iPhone4,1" on iPhone 4S
@"iPhone5,1" on iPhone 5 (model A1428, AT&T/Canada)
@"iPhone5,2" on iPhone 5 (model A1429, everything else)
@"iPad3,4" on 4th Generation iPad
@"iPad2,5" on iPad Mini
@"iPhone5,3" on iPhone 5c (model A1456, A1532 | GSM)
@"iPhone5,4" on iPhone 5c (model A1507, A1516, A1526 (China), A1529 | Global)
@"iPhone6,1" on iPhone 5s (model A1433, A1533 | GSM)
@"iPhone6,2" on iPhone 5s (model A1457, A1518, A1528 (China), A1530 | Global)
@"iPad4,1" on 5th Generation iPad (iPad Air) - Wifi
@"iPad4,2" on 5th Generation iPad (iPad Air) - Cellular
@"iPad4,4" on 2nd Generation iPad Mini - Wifi
@"iPad4,5" on 2nd Generation iPad Mini - Cellular
@"iPhone7,1" on iPhone 6 Plus
@"iPhone7,2" on iPhone 6
@"iPhone8,1" on iPhone 6S
@"iPhone8,2" on iPhone 6S Plus
Abhinav
  • 37,684
  • 43
  • 191
  • 309
3

Base on your file Device.swift, Insert:

    case iPhone6S
    case iPhone6SPlus

Under:

    case iPhone6Plus

and insert:

    case "iPhone8,1":                               self = iPhone6S
    case "iPhone8,2":                               self = iPhone6SPlus

under:

    case "iPhone7,1":                               self = iPhone6Plus
Luan Nguyen
  • 375
  • 3
  • 6
1

If you use AutoLayout you don't need to check which iPhone that your app is running into, the change will be made automatically.

Lamour
  • 3,002
  • 2
  • 16
  • 28
  • Yes I know that about the autolayout, but I ran into some issues that I had to make some adjustment, but it is too late now to change the design at this point. I agree is not the best thing I did. So for the first release I'll go with what I have and will make the changes on the next release. – borna Sep 19 '15 at 01:51
0

You can use this switch

public extension UIDevice {
var modelName: String {
    var systemInfo = utsname()
    uname(&systemInfo)
    let machineMirror = Mirror(reflecting: systemInfo.machine)
    let identifier = machineMirror.children.reduce("") { identifier, element in
        guard let value = element.value as? Int8, value != 0 else { return identifier }
        return identifier + String(UnicodeScalar(UInt8(value)))
    }

    switch identifier {
    case "iPhone3,1", "iPhone3,2", "iPhone3,3", "iPhone4,1":
        return "iPhone 4"

    case "iPhone5,1", "iPhone5,2", "iPhone5,3", "iPhone5,4", "iPhone6,1", "iPhone6,2", "iPhone8,4":
        return "iPhone 5"

    case "iPhone7,2", "iPhone8,1", "iPhone9,1", "iPhone9,3":
        return "iPhone 6,7"

    case "iPhone7,1", "iPhone8,2", "iPhone9,2", "iPhone9,4":
        return "iPhone Plus"

    case "i386", "x86_64":
        return "Simulator"
    default:
        return identifier
    }
}

}

An then i code:

switch UIDevice.current.modelName {
    case "iPhone 4":
        //Your code here
    case "iPhone 5":
        //Your code here
    case "iPhone 6,7":
        //Your code here
    case "iPhone Plus", "Simulator":
        //Your code here
    default:
        break
    }
Janserik
  • 2,306
  • 1
  • 24
  • 43