added iOS source code
[wl-app.git] / iOS / Pods / MatomoTracker / MatomoTracker / Device.swift
1 import Foundation
2
3 final public class Device: NSObject {
4     /// Creates an returns a new device object representing the current device
5     @objc public static func makeCurrentDevice() ->  Device {
6         let platform = currentPlatform()
7         let humanReadablePlatformName = humanReadablePlatformNameForCurrentDevice()
8         let os = osVersionForCurrentDevice()
9         let screenSize = screenSizeForCurrentDevice()
10         let nativeScreenSize = nativeScreenSizeForCurrentDevice()
11         return Device(platform: platform,
12                       humanReadablePlatformName: humanReadablePlatformName,
13                       osVersion: os,
14                       screenSize: screenSize,
15                       nativeScreenSize: nativeScreenSize)
16     }
17     
18     /// The platform name of the device i.e. "iPhone1,1" or "iPad3,6"
19     @objc public let platform: String
20     
21     /// A human readable version of the platform name i.e. "iPhone 6 Plus" or "iPad Air 2 (WiFi)"
22     /// Will be nil if no human readable string was found.
23     @objc public let humanReadablePlatformName: String?
24     
25     /// The version number of the OS as String i.e. "1.2" or "9.4"
26     @objc public let osVersion: String
27     
28     /// The screen size
29     @objc public let screenSize: CGSize
30     
31     /// The native screen size
32     /// Will be CGSize.zero if the value is not defined on the running platorm.
33     @objc public let nativeScreenSize: CGSize
34
35     required public init(platform: String, humanReadablePlatformName: String? = nil, osVersion: String, screenSize: CGSize, nativeScreenSize: CGSize? = nil) {
36         self.platform = platform
37         self.humanReadablePlatformName = humanReadablePlatformName
38         self.osVersion = osVersion
39         self.screenSize = screenSize
40         self.nativeScreenSize = nativeScreenSize != nil ? nativeScreenSize! : CGSize.zero
41
42         super.init()
43     }
44 }
45
46 extension Device {
47     /// The platform name of the current device i.e. "iPhone1,1" or "iPad3,6"
48     private static func currentPlatform() -> String  {
49         var size = 0
50         sysctlbyname("hw.machine", nil, &size, nil, 0)
51         var machine = [CChar](repeating: 0,  count: Int(size))
52         sysctlbyname("hw.machine", &machine, &size, nil, 0)
53         return String(cString: machine)
54     }
55     
56     /// Returns a human readable version of the current platform name i.e. "iPhone 6 Plus" or "iPad Air 2 (WiFi)"
57     /// Will return nil, if no human readable string could be found for the current platform
58     private static func humanReadablePlatformNameForCurrentDevice() -> String? {
59         let platform = currentPlatform()
60         switch platform {
61             
62         // iPhone
63         case "iPhone1,1":    return "iPhone 1G"
64         case "iPhone1,2":    return "iPhone 3G"
65         case "iPhone2,1":    return "iPhone 3GS"
66         case "iPhone3,1":    return "iPhone 4"
67         case "iPhone3,2":    return "iPhone 4 (Revision A)"
68         case "iPhone3,3":    return "Verizon iPhone 4"
69         case "iPhone4,1":    return "iPhone 4S"
70         case "iPhone5,1":    return "iPhone 5 (GSM)"
71         case "iPhone5,2":    return "iPhone 5 (GSM+CDMA)"
72         case "iPhone5,3":    return "iPhone 5c (GSM)"
73         case "iPhone5,4":    return "iPhone 5c (Global)"
74         case "iPhone6,1":    return "iPhone 5s (GSM)"
75         case "iPhone6,2":    return "iPhone 5s (Global)"
76         case "iPhone7,1":    return "iPhone 6 Plus"
77         case "iPhone7,2":    return "iPhone 6"
78         case "iPhone8,1":    return "iPhone 6s"
79         case "iPhone8,2":    return "iPhone 6s Plus"
80         case "iPhone8,4":    return "iPhone SE"
81         case "iPhone9,1":    return "iPhone 7 (GSM+CDMA)"
82         case "iPhone9,2":    return "iPhone 7 Plus (GSM+CDMA)"
83         case "iPhone9,3":    return "iPhone 7 (Global)"
84         case "iPhone9,4":    return "iPhone 7 Plus (Global)"
85         case "iPhone10,1":   return "iPhone 8 (GSM+CDMA)"
86         case "iPhone10,2":   return "iPhone 8 Plus (GSM+CDMA)"
87         case "iPhone10,3":   return "iPhone X (GSM+CDMA)"
88         case "iPhone10,4":   return "iPhone 8 (Global)"
89         case "iPhone10,5":   return "iPhone 8 Plus (Global)"
90         case "iPhone10,6":   return "iPhone X (Global)"
91             
92         // iPod
93         case "iPod1,1":      return "iPod Touch 1G"
94         case "iPod2,1":      return "iPod Touch 2G"
95         case "iPod3,1":      return "iPod Touch 3G"
96         case "iPod4,1":      return "iPod Touch 4G"
97         case "iPod5,1":      return "iPod Touch 5G"
98         case "iPod7,1":      return "iPod Touch 6G"
99             
100         // iPad
101         case "iPad1,1":      return "iPad 1"
102         case "iPad2,1":      return "iPad 2 (WiFi)"
103         case "iPad2,2":      return "iPad 2 (GSM)"
104         case "iPad2,3":      return "iPad 2 (CDMA)"
105         case "iPad2,4":      return "iPad 2 (WiFi)"
106         case "iPad2,5":      return "iPad Mini 1 (WiFi)"
107         case "iPad2,6":      return "iPad Mini 1 (GSM)"
108         case "iPad2,7":      return "iPad Mini 1 (GSM+CDMA)"
109         case "iPad3,1":      return "iPad 3 (WiFi)"
110         case "iPad3,2":      return "iPad 3 (GSM+CDMA)"
111         case "iPad3,3":      return "iPad 3 (GSM)"
112         case "iPad3,4":      return "iPad 4 (WiFi)"
113         case "iPad3,5":      return "iPad 4 (GSM)"
114         case "iPad3,6":      return "iPad 4 (GSM+CDMA)"
115         case "iPad4,1":      return "iPad Air 1 (WiFi)"
116         case "iPad4,2":      return "iPad Air 1 (Cellular)"
117         case "iPad4,3":      return "iPad Air"
118         case "iPad4,4":      return "iPad Mini 2 (WiFi)"
119         case "iPad4,5":      return "iPad Mini 2 (Cellular)"
120         case "iPad4,6":      return "iPad Mini 2 (Rev)"
121         case "iPad4,7":      return "iPad Mini 3 (WiFi)"
122         case "iPad4,8":      return "iPad Mini 3 (A1600)"
123         case "iPad4,9":      return "iPad Mini 3 (A1601)"
124         case "iPad5,1":      return "iPad Mini 4 (WiFi)"
125         case "iPad5,2":      return "iPad Mini 4 (Cellular)"
126         case "iPad5,3":      return "iPad Air 2 (WiFi)"
127         case "iPad5,4":      return "iPad Air 2 (Cellular)"
128         case "iPad6,3":      return "iPad Pro 9.7 (WiFi)"
129         case "iPad6,4":      return "iPad Pro 9.7 (Cellular)"
130         case "iPad6,7":      return "iPad Pro 12.9 (WiFi)"
131         case "iPad6,8":      return "iPad Pro 12.9 (Cellular)"
132             
133         // Apple Watch
134         case "Watch1,1":      return "Apple Watch 38mm"
135         case "Watch1,2":      return "Apple Watch 42mm"
136         case "Watch2,3":      return "Apple Watch 38mm (Series 2)"
137         case "Watch2,4":      return "Apple Watch 42mm (Series 2)"
138         case "Watch2,6":      return "Apple Watch 38mm (Series 1)"
139         case "Watch2,7":      return "Apple Watch 42mm (Series 1)"
140             
141         // Apple TV
142         case "AppleTV2,1":      return "Apple TV 2G"
143         case "AppleTV3,1":      return "Apple TV 3G"
144         case "AppleTV3,2":      return "Apple TV 3G (2013)"
145         case "AppleTV5,3":      return "Apple TV 4G"
146             
147         case "i386":         return "Simulator"
148         case "x86_64":       return "Simulator"
149             
150         default: return nil
151         }
152     }
153 }
154
155 #if os(OSX)
156     import AppKit
157     extension Device {
158         /// Returns the version number of the current OS as String i.e. "1.2" or "9.4"
159         internal static func osVersionForCurrentDevice() -> String  {
160             let version = ProcessInfo.processInfo.operatingSystemVersion
161             return "\(version.majorVersion).\(version.minorVersion).\(version.patchVersion)"
162         }
163         
164         // Returns the screen size in points
165         internal static func screenSizeForCurrentDevice() ->  CGSize {
166             guard let mainScreen = NSScreen.main else { return CGSize.zero }
167             return mainScreen.visibleFrame.size
168         }
169         
170         // Returns the screen size in pixels
171         internal static func nativeScreenSizeForCurrentDevice() ->  CGSize? {
172             return nil
173         }
174     }
175 #elseif os(iOS) || os(tvOS)
176     import UIKit
177     extension Device {
178         
179         /// Returns the version number of the current OS as String i.e. "1.2" or "9.4"
180         internal static func osVersionForCurrentDevice() -> String  {
181             return UIDevice.current.systemVersion
182         }
183         
184         // Returns the screen size in points
185         internal static func screenSizeForCurrentDevice() ->  CGSize {
186             let bounds = UIScreen.main.bounds
187             return bounds.size
188         }
189         
190         // Returns the screen size in pixels
191         internal static func nativeScreenSizeForCurrentDevice() ->  CGSize {
192             let bounds = UIScreen.main.nativeBounds
193             return bounds.size
194         }
195     }
196 #endif