NXDeviceManager.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //
  2. // NXDeviceManager.swift
  3. // NXFramework-Swift-Demo
  4. //
  5. // Created by ak on 2020/10/26.
  6. // Copyright © 2020 NXFramework-Swift. All rights reserved.
  7. //
  8. import UIKit
  9. class NXDeviceManager: NSObject {
  10. class func info() -> [String] {
  11. var data: [String] = []
  12. data.append("Device Name: \(deviceNameAlias())")
  13. if let bundleId = Bundle.main.bundleIdentifier {
  14. data.append("Bundle Identifier: \(bundleId)")
  15. }
  16. if let version = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String,
  17. let buildNumber = Bundle.main.infoDictionary?["CFBundleVersion"] as? String {
  18. data.append( "Host App Version: \(version).\(buildNumber)" )
  19. }
  20. if let venderId = UIDevice.current.identifierForVendor {
  21. data.append( "Identifier For Vendor: \(venderId)" )
  22. }
  23. data.append("System Version: \(getSystemVersion())")
  24. data.append("Model: \(platformModelString())")
  25. // data.append("Total Disk Space(MB): \(UIDevice.totalDiskSpaceInMB)")
  26. // data.append("Free Disk Space(MB): \(UIDevice.freeDiskSpaceInMB)")
  27. let lastRestarted = Date(timeIntervalSince1970: TimeInterval(Date().timeIntervalSince1970 - Double(uptime())))
  28. data.append("Uptime: \(uptime())/\(lastRestarted)")
  29. return data
  30. }
  31. class var isIpad:Bool {
  32. if #available(iOS 8.0, *) {
  33. return UIScreen.main.traitCollection.userInterfaceIdiom == .pad
  34. } else {
  35. return UIDevice.current.userInterfaceIdiom == .pad
  36. }
  37. }
  38. class var isIphone:Bool {
  39. if #available(iOS 8.0, *) {
  40. return UIScreen.main.traitCollection.userInterfaceIdiom == .phone
  41. } else {
  42. return UIDevice.current.userInterfaceIdiom == .phone
  43. }
  44. }
  45. ///Name of the devices, like Baudins's Iphone
  46. class func deviceNameAlias() -> String {
  47. return UIDevice.current.name
  48. }
  49. class func processorCount() -> Int {
  50. return ProcessInfo.processInfo.activeProcessorCount
  51. }
  52. //Verion of the OS, like 9.0.1
  53. class func osVersion()-> String {
  54. return UIDevice.current.systemVersion;
  55. }
  56. class func platformModelString() -> String {
  57. if let key = "hw.machine".cString(using: String.Encoding.utf8) {
  58. var size: Int = 0
  59. sysctlbyname(key, nil, &size, nil, 0)
  60. var machine = [CChar](repeating: 0, count: Int(size))
  61. sysctlbyname(key, &machine, &size, nil, 0)
  62. return String(cString: machine)
  63. }
  64. return "Unknown"
  65. }
  66. /** uptime in seconds **/
  67. class func uptime() -> Int {
  68. var currentTime = time_t()
  69. var bootTime = timeval()
  70. var mib = [CTL_KERN, KERN_BOOTTIME]
  71. var size = MemoryLayout<timeval>.stride
  72. if sysctl(&mib, u_int(mib.count), &bootTime, &size, nil, 0) != -1 && bootTime.tv_sec != 0 {
  73. time(&currentTime)
  74. if (currentTime < bootTime.tv_sec) {
  75. return 0
  76. }
  77. return currentTime - bootTime.tv_sec
  78. }
  79. return 0
  80. }
  81. class func getScreenBrightness() -> CGFloat {
  82. return UIScreen.main.brightness
  83. }
  84. class func getPhysicalMemory() -> UInt64 {
  85. return ProcessInfo.processInfo.physicalMemory
  86. }
  87. class func getSystemVersion() -> String {
  88. return UIDevice.current.systemVersion
  89. }
  90. }