FIRCLSFABHost.m 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. #if TARGET_OS_WATCH
  16. #import <WatchKit/WatchKit.h>
  17. #elif TARGET_OS_IPHONE
  18. #import <UIKit/UIKit.h>
  19. #endif
  20. #include <sys/sysctl.h>
  21. #define FIRCLS_HOST_SYSCTL_BUFFER_SIZE (128)
  22. #pragma mark - OS Versions
  23. #pragma mark Private
  24. static NSString *FIRCLSHostSysctlEntry(const char *sysctlKey) {
  25. char buffer[FIRCLS_HOST_SYSCTL_BUFFER_SIZE];
  26. size_t bufferSize = FIRCLS_HOST_SYSCTL_BUFFER_SIZE;
  27. if (sysctlbyname(sysctlKey, buffer, &bufferSize, NULL, 0) != 0) {
  28. return nil;
  29. }
  30. return [NSString stringWithUTF8String:buffer];
  31. }
  32. #pragma mark Public
  33. NSOperatingSystemVersion FIRCLSHostGetOSVersion(void) {
  34. // works on macos(10.10), ios(8.0), watchos(2.0), tvos(9.0)
  35. if ([NSProcessInfo.processInfo respondsToSelector:@selector(operatingSystemVersion)]) {
  36. return [NSProcessInfo.processInfo operatingSystemVersion];
  37. }
  38. NSOperatingSystemVersion version = {0, 0, 0};
  39. #if TARGET_OS_IPHONE
  40. #if TARGET_OS_WATCH
  41. NSString *versionString = [[WKInterfaceDevice currentDevice] systemVersion];
  42. #else
  43. NSString *versionString = [[UIDevice currentDevice] systemVersion];
  44. #endif
  45. NSArray *parts = [versionString componentsSeparatedByString:@"."];
  46. if (parts.count > 0) {
  47. version.majorVersion = [[parts objectAtIndex:0] integerValue];
  48. }
  49. if ([parts count] > 1) {
  50. version.minorVersion = [[parts objectAtIndex:1] integerValue];
  51. }
  52. if ([parts count] > 2) {
  53. version.patchVersion = [[parts objectAtIndex:2] integerValue];
  54. }
  55. #endif
  56. return version;
  57. }
  58. NSString *FIRCLSHostOSBuildVersion(void) {
  59. return FIRCLSHostSysctlEntry("kern.osversion");
  60. }
  61. NSString *FIRCLSHostOSDisplayVersion(void) {
  62. NSOperatingSystemVersion version = FIRCLSHostGetOSVersion();
  63. return [NSString stringWithFormat:@"%ld.%ld.%ld", (long)version.majorVersion,
  64. (long)version.minorVersion, (long)version.patchVersion];
  65. }
  66. #pragma mark - Host Models
  67. #pragma mark Public
  68. NSString *FIRCLSHostModelInfo(void) {
  69. NSString *model = nil;
  70. #if TARGET_OS_SIMULATOR
  71. #if TARGET_OS_WATCH
  72. model = @"watchOS Simulator";
  73. #elif TARGET_OS_TV
  74. model = @"tvOS Simulator";
  75. #elif TARGET_OS_IPHONE
  76. switch ([[UIDevice currentDevice] userInterfaceIdiom]) {
  77. case UIUserInterfaceIdiomPhone:
  78. model = @"iOS Simulator (iPhone)";
  79. break;
  80. case UIUserInterfaceIdiomPad:
  81. model = @"iOS Simulator (iPad)";
  82. break;
  83. default:
  84. model = @"iOS Simulator (Unknown)";
  85. break;
  86. }
  87. #endif
  88. #elif TARGET_OS_EMBEDDED
  89. model = FIRCLSHostSysctlEntry("hw.machine");
  90. #else
  91. model = FIRCLSHostSysctlEntry("hw.model");
  92. #endif
  93. return model;
  94. }