NSProcessInfo_PECocoaBackports.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright (c) 2014 Petroules Corporation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * 1. Redistributions of source code must retain the above copyright notice, this
  8. * list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright notice,
  10. * this list of conditions and the following disclaimer in the documentation
  11. * and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  14. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  17. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  18. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  19. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  20. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  21. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  22. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. #import "NSProcessInfo_PECocoaBackports.h"
  25. #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
  26. #import <UIKit/UIKit.h>
  27. #elif defined(TARGET_OS_MAC) && TARGET_OS_MAC
  28. #import <CoreServices/CoreServices.h>
  29. #endif
  30. @interface NSProcessInfo (PECocoaBackportsPrivate)
  31. #if LOAD_OPERATING_SYSTEM_VERSION
  32. - (NSOperatingSystemVersion)PECocoaBackports_operatingSystemVersion;
  33. - (BOOL)PECocoaBackports_isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion)version;
  34. #endif
  35. @end
  36. @implementation NSProcessInfo (PECocoaBackportsPrivate)
  37. + (void)load
  38. {
  39. #if LOAD_OPERATING_SYSTEM_VERSION
  40. // Public API since OS X 10.10 (present since 10.9) and iOS 8.0
  41. class_addInstanceMethodIfNecessary([self class],
  42. NSSelectorFromString(@"operatingSystemVersion"),
  43. @selector(PECocoaBackports_operatingSystemVersion));
  44. // Public API since OS X 10.10 (present since 10.9) and iOS 8.0
  45. class_addInstanceMethodIfNecessary([self class],
  46. NSSelectorFromString(@"isOperatingSystemAtLeastVersion:"),
  47. @selector(PECocoaBackports_isOperatingSystemAtLeastVersion:));
  48. #endif
  49. }
  50. #if LOAD_OPERATING_SYSTEM_VERSION
  51. - (NSOperatingSystemVersion)PECocoaBackports_operatingSystemVersion
  52. {
  53. NSOperatingSystemVersion v = {0, 0, 0};
  54. SInt32 major = 0, minor = 0, patch = 0;
  55. #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
  56. #if !defined(__has_feature) || !__has_feature(objc_arc)
  57. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  58. #endif
  59. NSArray *parts = [[[UIDevice currentDevice] systemVersion] componentsSeparatedByString:@"."];
  60. major = parts.count > 0 ? [[parts objectAtIndex:0] intValue] : 0;
  61. minor = parts.count > 1 ? [[parts objectAtIndex:1] intValue] : 0;
  62. patch = parts.count > 2 ? [[parts objectAtIndex:2] intValue] : 0;
  63. #if !defined(__has_feature) || !__has_feature(objc_arc)
  64. [pool release];
  65. #endif
  66. #elif defined(TARGET_OS_MAC) && TARGET_OS_MAC
  67. #pragma clang diagnostic push
  68. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  69. if (Gestalt(gestaltSystemVersionMajor, &major) != noErr) return v;
  70. if (Gestalt(gestaltSystemVersionMinor, &minor) != noErr) return v;
  71. if (Gestalt(gestaltSystemVersionBugFix, &patch) != noErr) return v;
  72. #pragma clang diagnostic pop
  73. #endif
  74. v.majorVersion = major;
  75. v.minorVersion = minor;
  76. v.patchVersion = patch;
  77. return v;
  78. }
  79. - (BOOL)PECocoaBackports_isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion)version
  80. {
  81. const NSOperatingSystemVersion systemVersion = [self operatingSystemVersion];
  82. if (systemVersion.majorVersion == version.majorVersion) {
  83. if (systemVersion.minorVersion == version.minorVersion) {
  84. return systemVersion.patchVersion >= version.patchVersion;
  85. }
  86. return systemVersion.minorVersion >= version.minorVersion;
  87. }
  88. return systemVersion.majorVersion >= version.majorVersion;
  89. }
  90. #endif
  91. @end