简体   繁体   中英

how to detect iPhone 6s and 6s+

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

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

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

如果您使用AutoLayout ,则无需检查应用程序所在的iPhone,将自动进行更改。

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
    }

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