Skip to content

Commit

Permalink
Handle mac type
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Baranes committed Mar 22, 2017
1 parent d8ad3c0 commit d78b402
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 117 deletions.
11 changes: 6 additions & 5 deletions Device.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
BE11EF091BE3FE9500816835 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Example/Base.lproj/Main.storyboard; sourceTree = SOURCE_ROOT; };
BE11EF0F1BE4001A00816835 /* Version.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Version.swift; sourceTree = "<group>"; };
BE11EF111BE4002800816835 /* Size.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Size.swift; sourceTree = "<group>"; };
E2C023AA1D6331010033AD25 /* Device macOS.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = "Device macOS.framework"; sourceTree = BUILT_PRODUCTS_DIR; };
E2C023AA1D6331010033AD25 /* Device_macOS.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Device_macOS.framework; sourceTree = BUILT_PRODUCTS_DIR; };
E2C023AC1D6331010033AD25 /* Device macOS.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Device macOS.h"; sourceTree = "<group>"; };
E2C023AE1D6331010033AD25 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
E2C023B61D6331A00033AD25 /* Device.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Device.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -122,7 +122,7 @@
children = (
BE11EEE71BE3FC0300816835 /* Example.app */,
001C44691C3C08C000F6599D /* Device.framework */,
E2C023AA1D6331010033AD25 /* Device macOS.framework */,
E2C023AA1D6331010033AD25 /* Device_macOS.framework */,
);
name = Products;
sourceTree = "<group>";
Expand Down Expand Up @@ -252,7 +252,7 @@
);
name = "Device macOS";
productName = "Device macOS";
productReference = E2C023AA1D6331010033AD25 /* Device macOS.framework */;
productReference = E2C023AA1D6331010033AD25 /* Device_macOS.framework */;
productType = "com.apple.product-type.framework";
};
/* End PBXNativeTarget section */
Expand Down Expand Up @@ -582,7 +582,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/Frameworks";
MACOSX_DEPLOYMENT_TARGET = 10.11;
PRODUCT_BUNDLE_IDENTIFIER = "com.tbaranes.Device-macOS";
PRODUCT_NAME = "$(TARGET_NAME)";
PRODUCT_NAME = Device_macOS;
SDKROOT = macosx;
SKIP_INSTALL = YES;
VERSIONING_SYSTEM = "apple-generic";
Expand All @@ -607,7 +607,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/Frameworks";
MACOSX_DEPLOYMENT_TARGET = 10.11;
PRODUCT_BUNDLE_IDENTIFIER = "com.tbaranes.Device-macOS";
PRODUCT_NAME = "$(TARGET_NAME)";
PRODUCT_NAME = Device_macOS;
SDKROOT = macosx;
SKIP_INSTALL = YES;
VERSIONING_SYSTEM = "apple-generic";
Expand Down Expand Up @@ -652,6 +652,7 @@
E2C023B01D6331010033AD25 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
Expand Down
151 changes: 40 additions & 111 deletions Source/DeviceMacOS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,133 +10,62 @@ import Cocoa

public class Device {
static private func getVersionCode() -> String {
var systemInfo = utsname()
uname(&systemInfo)

let versionCode: String = String(UTF8String: NSString(bytes: &systemInfo.machine, length: Int(_SYS_NAMELEN), encoding: NSASCIIStringEncoding)!.UTF8String)!

return versionCode
}

static private func getVersion(code code: String) -> Version {
switch code {


default: return Version.Unknown
}
var size : Int = 0
sysctlbyname("hw.model", nil, &size, nil, 0)
var model = [CChar](count: Int(size), repeatedValue: 0)
sysctlbyname("hw.model", &model, &size, nil, 0)
return String.fromCString(model) ?? ""
}

static private func getType(code code: String) -> Type {
let versionCode = Device.getVersionCode()

switch versionCode {
case "iPhone3,1", "iPhone3,2", "iPhone3,3",
"iPhone4,1", "iPhone4,2", "iPhone4,3",
"iPhone5,1", "iPhone5,2",
"iPhone5,3", "iPhone5,4",
"iPhone6,1", "iPhone6,2",
"iPhone7,2",
"iPhone7,1",
"iPhone8,1",
"iPhone8,2",
"iPhone8,4": return Type.iPhone

case "iPad1,1",
"iPad2,1", "iPad2,2", "iPad2,3", "iPad2,4",
"iPad3,1", "iPad3,2", "iPad3,3",
"iPad3,4", "iPad3,5", "iPad3,6",
"iPad4,1", "iPad4,2", "iPad4,3",
"iPad5,3", "iPad5,4",
"iPad2,5", "iPad2,6", "iPad2,7",
"iPad4,4", "iPad4,5", "iPad4,6",
"iPad4,7", "iPad4,8", "iPad4,9",
"iPad5,1", "iPad5,2",
"iPad6,3", "iPad6,4", "iPad6,7", "iPad6,8": return Type.iPad

case "iPod1,1",
"iPod2,1",
"iPod3,1",
"iPod4,1",
"iPod5,1",
"iPod7,1":
return Type.iPod
case "i386", "x86_64": return Type.Simulator
default: return Type.Unknown
if versionCode.hasPrefix("MacPro") {
return Type.MacPro
} else if versionCode.hasPrefix("iMac") {
return Type.iMac
} else if versionCode.hasPrefix("MacBookPro") {
return Type.MacBookPro
} else if versionCode.hasPrefix("MacBookAir") {
return Type.MacBookAir
} else if versionCode.hasPrefix("MacBook") {
return Type.MacBook
}
}


static public func version() -> Version {
let versionName = Device.getVersionCode()

return Device.getVersion(code: versionName)
return Type.Unknown
}

static public func size() -> Size {
let w: Double = Double(CGRectGetWidth(UIScreen.mainScreen().bounds))
let h: Double = Double(CGRectGetHeight(UIScreen.mainScreen().bounds))
let screenHeight: Double = max(w, h)

switch screenHeight {
case 480:
return Size.Screen3_5Inch
case 568:
return Size.Screen4Inch
case 667:
return UIScreen.mainScreen().scale == 3.0 ? Size.Screen5_5Inch : Size.Screen4_7Inch
case 736:
return Size.Screen5_5Inch
case 1024:
switch Device.version() {
case .iPadMini,.iPadMini2,.iPadMini3,.iPadMini4:
return Size.Screen7_9Inch
default:
return Size.Screen9_7Inch
}
case 1366:
return Size.Screen12_9Inch
let screen = NSScreen.mainScreen()
let description = screen?.deviceDescription
let displayPhysicalSize = CGDisplayScreenSize(description?["NSScreenNumber"] as? CGDirectDisplayID ?? 0)
let sizeInInches = floor(sqrt(pow(displayPhysicalSize.width, 2) + pow(displayPhysicalSize.height, 2)) * 0.0393701);

switch sizeInInches {
case 11:
return Size.Screen11Inch
case 12:
return Size.Screen12Inch
case 13:
return Size.Screen13Inch
case 15:
return Size.Screen15Inch
case 17:
return Size.Screen17Inch
case 20:
return Size.Screen20Inch
case 21:
return Size.Screen21_5Inch
case 24:
return Size.Screen24Inch
case 27:
return Size.Screen27Inch
default:
return Size.UnknownSize
}
}

static public func type() -> Type {
let versionName = Device.getVersionCode()

return Device.getType(code: versionName)
}

static public func isEqualToScreenSize(size: Size) -> Bool {
return size == Device.size() ? true : false;
}

static public func isLargerThanScreenSize(size: Size) -> Bool {
return size.rawValue < Device.size().rawValue ? true : false;
}

static public func isSmallerThanScreenSize(size: Size) -> Bool {
return size.rawValue > Device.size().rawValue ? true : false;
}

static public func isRetina() -> Bool {
return UIScreen.mainScreen().scale > 1.0
}

static public func isPad() -> Bool {
return Device.type() == .iPad
}

static public func isPhone() -> Bool {
return Device.type() == .iPhone

}

static public func isPod() -> Bool {
return Device.type() == .iPod
}

static public func isSimulator() -> Bool {
return Device.type() == .Simulator
}

}
2 changes: 2 additions & 0 deletions Source/Size.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ public enum Size: Int, Comparable {
case Screen12Inch
case Screen13Inch
case Screen15Inch
case Screen17Inch
case Screen20Inch
case Screen21_5Inch
case Screen24Inch
case Screen27Inch
#endif
}

Expand Down
2 changes: 1 addition & 1 deletion Source/Type.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public enum Type: String {
case iPad
case iPod
case simulator
#elseif(OSX)
#elseif os(OSX)
case iMac
case MacMini
case MacPro
Expand Down

0 comments on commit d78b402

Please sign in to comment.