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

前端 未结 13 664
一整个雨季
一整个雨季 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:51

    import Foundation
    import UIKit
    
    public enum IADisplayType {
        // unknow device
        case        unspecified
        // iPhone
        case        unknowiPhone
        case        iPhone3GS
        case        iPhone4
        static let  iPhone4s            = iPhone5
        case        iPhone5
        static let  iPhone5s            = iPhone5
        static let  iPhoneSE            = iPhone5
        case        iPhone6
        case        iPhone6Plus
        static let  iPhone6s            = iPhone6
        static let  iPhone6sPlus        = iPhone6Plus
        static let  iPhone7             = iPhone6
        static let  iPhone7Plus         = iPhone6Plus
        static let  iPhone8             = iPhone6
        static let  iPhone8Plus         = iPhone6Plus
        case        iPhoneX
        // iPad
        case        unknowiPad
        case        iPad79
        static let  iPad97              = iPad79
        case        iPad105
        case        iPad129
        // Apple CarPlay
        case        carPlay
        // Apple TV
        case        tv
    }
    
    public final class IADisplayManager {
    
        // MARK: - public interface
        // singleton
        static var shared: IADisplayManager {
            get {
                return IADisplayManager()
            }
        }
    
        // get current device type
        public var currentType: IADisplayType {
            get {
                return calCurrentType()
            }
        }
    
        // device current Native Resolution
        public var nativeResolution: CGSize {
            get {
                return UIScreen.main.nativeBounds.size
            }
        }
    
        // device current Native Scale Factor
        public var nativeScaleFactor: CGFloat {
            get {
                return UIScreen.main.nativeScale
            }
        }
    
        // device current Interface Idiom
        public var interfaceIdiom: UIUserInterfaceIdiom {
            get {
                return UIDevice().userInterfaceIdiom
            }
        }
    
        fileprivate init() {}
    
        // MARK: - private interface
        fileprivate func calCurrentType() -> IADisplayType {
            typealias Type = IADisplayType
    
            // unknown device
            if interfaceIdiom == .unspecified { return Type.unspecified }
            // iPhone && iPod Touch
            else if interfaceIdiom == .phone {
                if nativeScaleFactor == 1.0 && nativeResolution == CGSize(width: 320, height: 480) { return Type.iPhone3GS }
                else if nativeScaleFactor == 2.0 && nativeResolution == CGSize(width: 640, height: 960) { return Type.iPhone4 }
                else if nativeScaleFactor == 2.0 && nativeResolution == CGSize(width: 640, height: 1136) { return Type.iPhone5 }
                else if nativeScaleFactor == 2.0 && nativeResolution == CGSize(width: 750, height: 1334) { return Type.iPhone6 }
                else if (nativeScaleFactor-2.608) < 0.001 && nativeResolution == CGSize(width: 1080, height: 1920) { return Type.iPhone6Plus }
                else if nativeScaleFactor == 3.0 && nativeResolution == CGSize(width: 1125, height: 2436) { return Type.iPhoneX }
                else { return Type.unknowiPhone }
            }
            // iPad
            else if interfaceIdiom == .pad {
                if nativeScaleFactor == 2.0 && nativeResolution == CGSize(width: 1536, height: 2048) { return Type.iPad79 }
                else if nativeScaleFactor == 2.0 && nativeResolution == CGSize(width: 2224, height: 1668) { return Type.iPad105 }
                else if nativeScaleFactor == 2.0 && nativeResolution == CGSize(width: 2048, height: 2732) { return Type.iPad129 }
                else { return Type.unknowiPad }
            }
            // Apple CarPlay
            else if interfaceIdiom == .carPlay { return Type.carPlay }
            // Apple TV
            else if interfaceIdiom == .tv { return Type.tv }
            // unknown device
            else { return Type.unspecified }
        }
    
    }
    

提交回复
热议问题