iOS How to detect iPhone X, iPhone 6 plus, iPhone 6, iPhone 5, iPhone 4 by macro?

前端 未结 13 619
一整个雨季
一整个雨季 2020-11-28 01:17

How to detect device model by macro? i had using something like this but the result on the simulator alway IS_IPHONE_5

#define IS_IPAD (UI_USER_INTERFACE_IDI         


        
相关标签:
13条回答
  • 2020-11-28 01:35

    It's better not to bind your code with device types. This will lead to inflexible convoluted one. Apple wants you to think about sizes not devices. In case you need to have special sizes for devices larger than iPhone 5 such as image or font sizes, I would recommend to create a normalize class with a multiplier that grows your base size by a certain percent based on the relation between the iPhone 5 screen width and the current device size.

    let BaseWidth : CGFloat = 320
    
    class Normalizer: NSObject {
        //scale value proportional to the screen width
        class func normalize(value:CGFloat,multiplier : CGFloat = 1,maxDelta:CGFloat = 1024) -> CGFloat{
            let screenWidth = UIScreen.mainScreen().bounds.size.width
            let percent = (screenWidth - BaseWidth)/screenWidth
            let normalizedValue = value * (1 + percent) * multiplier
            return min(normalizedValue, value + maxDelta)//capped by a max value if needed
        } 
    }
    

    So in your code you will do something like that:

    value = Normalizer.normalize(30)
    
    0 讨论(0)
  • 2020-11-28 01:36

    For Swift:

    struct ScreenSize
    {
        static let SCREEN_WIDTH = UIScreen.main.bounds.size.width
        static let SCREEN_HEIGHT = UIScreen.main.bounds.size.height
        static let SCREEN_MAX_LENGTH = max(ScreenSize.SCREEN_WIDTH, ScreenSize.SCREEN_HEIGHT)
        static let SCREEN_MIN_LENGTH = min(ScreenSize.SCREEN_WIDTH, ScreenSize.SCREEN_HEIGHT)
    }
    
    struct DeviceType
    {
        static let IS_IPHONE_4_OR_LESS =  UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH < 568.0
        static let IS_IPHONE_5 = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 568.0
        static let IS_IPHONE_6 = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 667.0
        static let IS_IPHONE_6P = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 736.0
    }
    
    0 讨论(0)
  • 2020-11-28 01:38
    #define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    #define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) // iPhone and       iPod touch style UI
    
    #define IS_IPHONE_5_IOS7 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 568.0f)
    #define IS_IPHONE_6_IOS7 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 667.0f)
    #define IS_IPHONE_6P_IOS7 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 736.0f)
    #define IS_IPHONE_4_AND_OLDER_IOS7 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height < 568.0f)
    
    #define IS_IPHONE_5_IOS8 (IS_IPHONE && ([[UIScreen mainScreen] nativeBounds].size.height/[[UIScreen mainScreen] nativeScale]) == 568.0f)
    #define IS_IPHONE_6_IOS8 (IS_IPHONE && ([[UIScreen mainScreen] nativeBounds].size.height/[[UIScreen mainScreen] nativeScale]) == 667.0f)
    #define IS_IPHONE_6P_IOS8 (IS_IPHONE && ([[UIScreen mainScreen] nativeBounds].size.height/[[UIScreen mainScreen] nativeScale]) == 736.0f)
    #define IS_IPHONE_4_AND_OLDER_IOS8 (IS_IPHONE && ([[UIScreen mainScreen] nativeBounds].size.height/[[UIScreen mainScreen] nativeScale]) < 568.0f)
    
    #define IS_IPHONE_5 ( ( [ [ UIScreen mainScreen ] respondsToSelector: @selector( nativeBounds ) ] ) ? IS_IPHONE_5_IOS8 : IS_IPHONE_5_IOS7 )
    #define IS_IPHONE_6 ( ( [ [ UIScreen mainScreen ] respondsToSelector: @selector( nativeBounds ) ] ) ? IS_IPHONE_6_IOS8 : IS_IPHONE_6_IOS7 )
    #define IS_IPHONE_6P ( ( [ [ UIScreen mainScreen ] respondsToSelector: @selector( nativeBounds ) ] ) ? IS_IPHONE_6P_IOS8 : IS_IPHONE_6P_IOS7 )
    #define IS_IPHONE_4_AND_OLDER ( ( [ [ UIScreen mainScreen ] respondsToSelector: @selector( nativeBounds ) ] ) ? IS_IPHONE_4_AND_OLDER_IOS8 : IS_IPHONE_4_AND_OLDER_IOS7 )
    
    0 讨论(0)
  • 2020-11-28 01:40

    I can confirm the bug goes away when you set a Launch Screen.

    I kept my launch images, and added MainStoryboard to Launch Screen and the simulator recognized the devices correctly.

    0 讨论(0)
  • 2020-11-28 01:42
    //Device Type enum
    enum DeviceType: Int {
    
    //Apple UnknownDevices
    case UnknownDevice = 0
    
    //Simulator
    case Simulator
    
    //Apple Air pods
    case AppleAirPods
    
    //Apple TV
    case AppleTV2G
    case AppleTV3G
    case AppleTV4G
    case AppleTV4K
    
    //Apple Watch
    case AppleWatch
    case AppleWatchSeries1
    case AppleWatchSeries2
    case AppleWatchSeries3
    
    //Apple Home Pods
    case AppleHomePods
    
    //Apple iPad
    case AppleIpad
    case AppleIpad2
    case AppleIpad3
    case AppleIpad4
    case AppleIpadAir
    case AppleIpadAir2
    case AppleIpadPro_12_9
    case AppleIpadPro_9_7
    case AppleIpad5
    case AppleIpadPro_12_9_Gen_2
    case AppleIpadPro_10_5
    case AppleIpadMini
    case AppleIpadMini2
    case AppleIpadMini3
    case AppleIpadMini4
    
    //Apple iPhone
    case AppleIphone
    case AppleIphone3G
    case AppleIphone3GS
    case AppleIphone4
    case AppleIphone4S
    case AppleIphone5
    case AppleIphone5C
    case AppleIphone5S
    case AppleIphone6
    case AppleIphone6P
    case AppleIphone6S
    case AppleIphone6SP
    case AppleIphoneSE
    case AppleIphone7
    case AppleIphone7P
    case AppleIphone8
    case AppleIphone8P
    case AppleIphoneX
    
    //Apple iPod touch
    case AppleIpodTouch
    case AppleIpodTouch2G
    case AppleIpodTouch3G
    case AppleIpodTouch4G
    case AppleIpodTouch5G
    case AppleIpodTouch6G
    }
    
    // Method for device type
    func getDeviceType() -> DeviceType{
        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 {
    
        //Simulator
        case "i386","x86_64": return .Simulator
    
        //Apple Air Pods
        case "AirPods1,1": return .AppleAirPods
    
        //Apple TV
        case "AppleTV2,1": return .AppleTV2G
        case "AppleTV3,1", "AppleTV3,2": return .AppleTV3G
        case "AppleTV5,3": return .AppleTV4G
        case "AppleTV6,2": return .AppleTV4K
    
        //Apple Watch
        case "Watch1,1", "Watch1,2": return .AppleWatch
        case "Watch2,6", "Watch2,7": return .AppleWatchSeries1
        case "Watch2,3", "Watch2,4": return .AppleWatchSeries2
        case "Watch3,1", "Watch3,2", "Watch3,3", "Watch3,4": return .AppleWatchSeries3
    
        // Apple HomePods
        case "AudioAccessory1,1": return .AppleHomePods
    
        //Apple iPad
        case "iPad1,1": return .AppleIpad
        case "iPad2,1", "iPad2,2", "iPad2,3", "iPad2,4": return .AppleIpad2
        case "iPad3,1", "iPad3,2", "iPad3,3": return .AppleIpad3
        case "iPad3,4", "iPad3,5", "iPad3,6": return .AppleIpad4
        case "iPad4,1", "iPad4,2", "iPad4,3": return .AppleIpadAir
        case "iPad5,3", "iPad5,4": return .AppleIpadAir2
        case "iPad6,7", "iPad6,8": return .AppleIpadPro_12_9
        case "iPad6,3", "iPad6,4": return .AppleIpadPro_9_7
        case "iPad6,11", "iPad6,12": return .AppleIpad5
        case "iPad7,1", "iPad7,2" : return .AppleIpadPro_12_9_Gen_2
        case "iPad7,3", "iPad7,4" : return .AppleIpadPro_10_5
        case "iPad2,5", "iPad2,6", "iPad2,7": return .AppleIpadMini
        case "iPad4,4", "iPad4,5", "iPad4,6": return .AppleIpadMini2
        case "iPad4,7", "iPad4,8", "iPad4,9": return .AppleIpadMini3
        case "iPad5,1", "iPad5,2": return .AppleIpadMini4
    
        //Apple iPhone
        case "iPhone1,1": return .AppleIphone
        case "iPhone1,2": return .AppleIphone3G
        case "iPhone2,1": return .AppleIphone3GS
        case "iPhone3,1", "iPhone3,2", "iPhone3,3": return .AppleIphone4
        case "iPhone4,1": return .AppleIphone4S
        case "iPhone5,1", "iPhone5,2": return .AppleIphone5
        case "iPhone5,3", "iPhone5,4": return .AppleIphone5C
        case "iPhone6,1", "iPhone6,2": return .AppleIphone5S
        case "iPhone7,2": return .AppleIphone6
        case "iPhone7,1": return .AppleIphone6P
        case "iPhone8,1": return .AppleIphone6S
        case "iPhone8,2": return .AppleIphone6SP
        case "iPhone8,4": return .AppleIphoneSE
        case "iPhone9,1", "iPhone9,3": return .AppleIphone7
        case "iPhone9,2", "iPhone9,4": return .AppleIphone7P
        case "iPhone10,1", "iPhone10,4": return .AppleIphone8
        case "iPhone10,2", "iPhone10,5": return .AppleIphone8P
        case "iPhone10,3", "iPhone10,6": return .AppleIphoneX
    
        //Apple iPod touch
        case "iPod1,1": return .AppleIpodTouch
        case "iPod2,1": return .AppleIpodTouch2G
        case "iPod3,1": return .AppleIpodTouch3G
        case "iPod4,1": return .AppleIpodTouch4G
        case "iPod5,1": return .AppleIpodTouch5G
        case "iPod7,1": return .AppleIpodTouch6G
    
        default:
            return .UnknownDevice
        }
    }
    
    0 讨论(0)
  • 2020-11-28 01:45

    UIDeivce extension with Swift 3+ syntax.

    public extension UIDevice {
    
        var iPhone: Bool {
            return UIDevice().userInterfaceIdiom == .phone
        }
    
        enum ScreenType: String {
            case iPhone4
            case iPhone5
            case iPhone6
            case iPhone6Plus
            case iPhoneX
            case Unknown
        }
        var screenType: ScreenType {
            guard iPhone else { return .Unknown}
            switch UIScreen.main.nativeBounds.height {
            case 960:
                return .iPhone4
            case 1136:
                return .iPhone5
            case 1334:
                return .iPhone6
            case 2208, 1920:
                return .iPhone6Plus
            case 2436:
                return .iPhoneX
            default:
                return .Unknown
            }
        }
    
    }
    

    Sample usage:

    switch UIDevice().screenType {
            case .iPhone4, .iPhone5:
                // Code for iPhone 4 & iPhone 5
                break
            case .iPhone6:
                // Code for iPhone 6 & iPhone 7
                break
            case .iPhone6Plus:
                // Code for iPhone 6 Plus & iPhone 7 Plus
                break
            case .iPhoneX:
                // Code for iPhone X
                break
            default:
                break
            }
    

    Original answer: https://stackoverflow.com/a/36479017/3659227

    0 讨论(0)
提交回复
热议问题