UIDevice+YYAdd.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. //
  2. // UIDevice+YYAdd.h
  3. // YYKit <https://github.com/ibireme/YYKit>
  4. //
  5. // Created by ibireme on 13/4/3.
  6. // Copyright (c) 2015 ibireme.
  7. //
  8. // This source code is licensed under the MIT-style license found in the
  9. // LICENSE file in the root directory of this source tree.
  10. //
  11. #import <UIKit/UIKit.h>
  12. NS_ASSUME_NONNULL_BEGIN
  13. /**
  14. Provides extensions for `UIDevice`.
  15. */
  16. @interface UIDevice (YYAdd)
  17. #pragma mark - Device Information
  18. ///=============================================================================
  19. /// @name Device Information
  20. ///=============================================================================
  21. /// Device system version (e.g. 8.1)
  22. + (double)systemVersion;
  23. /// Whether the device is iPad/iPad mini.
  24. @property (nonatomic, readonly) BOOL isPad;
  25. /// Whether the device is a simulator.
  26. @property (nonatomic, readonly) BOOL isSimulator;
  27. /// Whether the device is jailbroken.
  28. @property (nonatomic, readonly) BOOL isJailbroken;
  29. /// Wherher the device can make phone calls.
  30. @property (nonatomic, readonly) BOOL canMakePhoneCalls NS_EXTENSION_UNAVAILABLE_IOS("");
  31. /// The device's machine model. e.g. "iPhone6,1" "iPad4,6"
  32. /// @see http://theiphonewiki.com/wiki/Models
  33. @property (nullable, nonatomic, readonly) NSString *machineModel;
  34. /// The device's machine model name. e.g. "iPhone 5s" "iPad mini 2"
  35. /// @see http://theiphonewiki.com/wiki/Models
  36. @property (nullable, nonatomic, readonly) NSString *machineModelName;
  37. /// The System's startup time.
  38. @property (nonatomic, readonly) NSDate *systemUptime;
  39. #pragma mark - Network Information
  40. ///=============================================================================
  41. /// @name Network Information
  42. ///=============================================================================
  43. /// WIFI IP address of this device (can be nil). e.g. @"192.168.1.111"
  44. @property (nullable, nonatomic, readonly) NSString *ipAddressWIFI;
  45. /// Cell IP address of this device (can be nil). e.g. @"10.2.2.222"
  46. @property (nullable, nonatomic, readonly) NSString *ipAddressCell;
  47. /**
  48. Network traffic type:
  49. WWAN: Wireless Wide Area Network.
  50. For example: 3G/4G.
  51. WIFI: Wi-Fi.
  52. AWDL: Apple Wireless Direct Link (peer-to-peer connection).
  53. For exmaple: AirDrop, AirPlay, GameKit.
  54. */
  55. typedef NS_OPTIONS(NSUInteger, YYNetworkTrafficType) {
  56. YYNetworkTrafficTypeWWANSent = 1 << 0,
  57. YYNetworkTrafficTypeWWANReceived = 1 << 1,
  58. YYNetworkTrafficTypeWIFISent = 1 << 2,
  59. YYNetworkTrafficTypeWIFIReceived = 1 << 3,
  60. YYNetworkTrafficTypeAWDLSent = 1 << 4,
  61. YYNetworkTrafficTypeAWDLReceived = 1 << 5,
  62. YYNetworkTrafficTypeWWAN = YYNetworkTrafficTypeWWANSent | YYNetworkTrafficTypeWWANReceived,
  63. YYNetworkTrafficTypeWIFI = YYNetworkTrafficTypeWIFISent | YYNetworkTrafficTypeWIFIReceived,
  64. YYNetworkTrafficTypeAWDL = YYNetworkTrafficTypeAWDLSent | YYNetworkTrafficTypeAWDLReceived,
  65. YYNetworkTrafficTypeALL = YYNetworkTrafficTypeWWAN |
  66. YYNetworkTrafficTypeWIFI |
  67. YYNetworkTrafficTypeAWDL,
  68. };
  69. /**
  70. Get device network traffic bytes.
  71. @discussion This is a counter since the device's last boot time.
  72. Usage:
  73. uint64_t bytes = [[UIDevice currentDevice] getNetworkTrafficBytes:YYNetworkTrafficTypeALL];
  74. NSTimeInterval time = CACurrentMediaTime();
  75. uint64_t bytesPerSecond = (bytes - _lastBytes) / (time - _lastTime);
  76. _lastBytes = bytes;
  77. _lastTime = time;
  78. @param type traffic types
  79. @return bytes counter.
  80. */
  81. - (uint64_t)getNetworkTrafficBytes:(YYNetworkTrafficType)types;
  82. #pragma mark - Disk Space
  83. ///=============================================================================
  84. /// @name Disk Space
  85. ///=============================================================================
  86. /// Total disk space in byte. (-1 when error occurs)
  87. @property (nonatomic, readonly) int64_t diskSpace;
  88. /// Free disk space in byte. (-1 when error occurs)
  89. @property (nonatomic, readonly) int64_t diskSpaceFree;
  90. /// Used disk space in byte. (-1 when error occurs)
  91. @property (nonatomic, readonly) int64_t diskSpaceUsed;
  92. #pragma mark - Memory Information
  93. ///=============================================================================
  94. /// @name Memory Information
  95. ///=============================================================================
  96. /// Total physical memory in byte. (-1 when error occurs)
  97. @property (nonatomic, readonly) int64_t memoryTotal;
  98. /// Used (active + inactive + wired) memory in byte. (-1 when error occurs)
  99. @property (nonatomic, readonly) int64_t memoryUsed;
  100. /// Free memory in byte. (-1 when error occurs)
  101. @property (nonatomic, readonly) int64_t memoryFree;
  102. /// Acvite memory in byte. (-1 when error occurs)
  103. @property (nonatomic, readonly) int64_t memoryActive;
  104. /// Inactive memory in byte. (-1 when error occurs)
  105. @property (nonatomic, readonly) int64_t memoryInactive;
  106. /// Wired memory in byte. (-1 when error occurs)
  107. @property (nonatomic, readonly) int64_t memoryWired;
  108. /// Purgable memory in byte. (-1 when error occurs)
  109. @property (nonatomic, readonly) int64_t memoryPurgable;
  110. #pragma mark - CPU Information
  111. ///=============================================================================
  112. /// @name CPU Information
  113. ///=============================================================================
  114. /// Avaliable CPU processor count.
  115. @property (nonatomic, readonly) NSUInteger cpuCount;
  116. /// Current CPU usage, 1.0 means 100%. (-1 when error occurs)
  117. @property (nonatomic, readonly) float cpuUsage;
  118. /// Current CPU usage per processor (array of NSNumber), 1.0 means 100%. (nil when error occurs)
  119. @property (nullable, nonatomic, readonly) NSArray<NSNumber *> *cpuUsagePerProcessor;
  120. @end
  121. NS_ASSUME_NONNULL_END
  122. #ifndef kSystemVersion
  123. #define kSystemVersion [UIDevice systemVersion]
  124. #endif
  125. #ifndef kiOS6Later
  126. #define kiOS6Later (kSystemVersion >= 6)
  127. #endif
  128. #ifndef kiOS7Later
  129. #define kiOS7Later (kSystemVersion >= 7)
  130. #endif
  131. #ifndef kiOS8Later
  132. #define kiOS8Later (kSystemVersion >= 8)
  133. #endif
  134. #ifndef kiOS9Later
  135. #define kiOS9Later (kSystemVersion >= 9)
  136. #endif