FIRCLSFABHost.m 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright 2019 Google
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "Crashlytics/Shared/FIRCLSFABHost.h"
  15. #import <GoogleUtilities/GULAppEnvironmentUtil.h>
  16. #if TARGET_OS_WATCH
  17. #import <WatchKit/WatchKit.h>
  18. #elif TARGET_OS_IPHONE
  19. #import <UIKit/UIKit.h>
  20. #endif
  21. #include <sys/sysctl.h>
  22. #define FIRCLS_HOST_SYSCTL_BUFFER_SIZE (128)
  23. #pragma mark - OS Versions
  24. #pragma mark Private
  25. static NSString *FIRCLSHostSysctlEntry(const char *sysctlKey) {
  26. char buffer[FIRCLS_HOST_SYSCTL_BUFFER_SIZE];
  27. size_t bufferSize = FIRCLS_HOST_SYSCTL_BUFFER_SIZE;
  28. if (sysctlbyname(sysctlKey, buffer, &bufferSize, NULL, 0) != 0) {
  29. return nil;
  30. }
  31. return [NSString stringWithUTF8String:buffer];
  32. }
  33. #pragma mark Public
  34. NSOperatingSystemVersion FIRCLSHostGetOSVersion(void) {
  35. // works on macos(10.10), ios(8.0), watchos(2.0), tvos(9.0)
  36. if ([NSProcessInfo.processInfo respondsToSelector:@selector(operatingSystemVersion)]) {
  37. return [NSProcessInfo.processInfo operatingSystemVersion];
  38. }
  39. NSOperatingSystemVersion version = {0, 0, 0};
  40. #if TARGET_OS_IPHONE
  41. #if TARGET_OS_WATCH
  42. NSString *versionString = [[WKInterfaceDevice currentDevice] systemVersion];
  43. #else
  44. NSString *versionString = [[UIDevice currentDevice] systemVersion];
  45. #endif
  46. NSArray *parts = [versionString componentsSeparatedByString:@"."];
  47. if (parts.count > 0) {
  48. version.majorVersion = [[parts objectAtIndex:0] integerValue];
  49. }
  50. if ([parts count] > 1) {
  51. version.minorVersion = [[parts objectAtIndex:1] integerValue];
  52. }
  53. if ([parts count] > 2) {
  54. version.patchVersion = [[parts objectAtIndex:2] integerValue];
  55. }
  56. #endif
  57. return version;
  58. }
  59. NSString *FIRCLSHostOSBuildVersion(void) {
  60. return FIRCLSHostSysctlEntry("kern.osversion");
  61. }
  62. NSString *FIRCLSHostOSDisplayVersion(void) {
  63. NSOperatingSystemVersion version = FIRCLSHostGetOSVersion();
  64. return [NSString stringWithFormat:@"%ld.%ld.%ld", (long)version.majorVersion,
  65. (long)version.minorVersion, (long)version.patchVersion];
  66. }
  67. #pragma mark - Host Models
  68. #pragma mark Public
  69. NSString *FIRCLSHostModelInfo(void) {
  70. return [GULAppEnvironmentUtil deviceSimulatorModel];
  71. }