iOS_utils.mm 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. * iOS_utils.mm, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "iOS_utils.h"
  11. #import <Foundation/Foundation.h>
  12. #import <UIKit/UIKit.h>
  13. namespace
  14. {
  15. NSString *standardPathNative(NSSearchPathDirectory directory)
  16. {
  17. return [NSFileManager.defaultManager URLForDirectory:directory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:NULL].path;
  18. }
  19. const char *standardPath(NSSearchPathDirectory directory) { return standardPathNative(directory).fileSystemRepresentation; }
  20. }
  21. namespace iOS_utils
  22. {
  23. const char *documentsPath() { return standardPath(NSDocumentDirectory); }
  24. const char *cachesPath() { return standardPath(NSCachesDirectory); }
  25. #if TARGET_OS_SIMULATOR
  26. const char *hostApplicationSupportPath()
  27. {
  28. static NSString *applicationSupportPath;
  29. static dispatch_once_t onceToken;
  30. dispatch_once(&onceToken, ^{
  31. auto cachesPath = standardPathNative(NSCachesDirectory);
  32. auto afterMacOsHomeDirPos = [cachesPath rangeOfString:@"Library/Developer"].location;
  33. NSCAssert(afterMacOsHomeDirPos != NSNotFound, @"simulator directory location is not under user's home directory: %@", cachesPath);
  34. applicationSupportPath = [[cachesPath substringToIndex:afterMacOsHomeDirPos] stringByAppendingPathComponent:@"Library/Application Support/vcmi"].stringByResolvingSymlinksInPath;
  35. });
  36. return applicationSupportPath.fileSystemRepresentation;
  37. }
  38. #endif
  39. const char *bundlePath() { return NSBundle.mainBundle.bundlePath.fileSystemRepresentation; }
  40. const char *frameworksPath() { return NSBundle.mainBundle.privateFrameworksPath.fileSystemRepresentation; }
  41. const char *bundleIdentifier() { return NSBundle.mainBundle.bundleIdentifier.UTF8String; }
  42. bool isOsVersionAtLeast(unsigned int osMajorVersion)
  43. {
  44. return NSProcessInfo.processInfo.operatingSystemVersion.majorVersion >= osMajorVersion;
  45. }
  46. void keepScreenOn(bool isEnabled)
  47. {
  48. UIApplication.sharedApplication.idleTimerDisabled = isEnabled ? YES : NO;
  49. }
  50. }