iOS_utils.mm 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. #import <MobileCoreServices/MobileCoreServices.h>
  14. #import <objc/message.h>
  15. #import <sys/utsname.h>
  16. namespace
  17. {
  18. NSString *standardPathNative(NSSearchPathDirectory directory)
  19. {
  20. return [NSFileManager.defaultManager URLForDirectory:directory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:NULL].path;
  21. }
  22. const char *standardPath(NSSearchPathDirectory directory) { return standardPathNative(directory).fileSystemRepresentation; }
  23. }
  24. namespace iOS_utils
  25. {
  26. const char *documentsPath() { return standardPath(NSDocumentDirectory); }
  27. const char *cachesPath() { return standardPath(NSCachesDirectory); }
  28. #if TARGET_OS_SIMULATOR
  29. const char *hostApplicationSupportPath()
  30. {
  31. static NSString *applicationSupportPath;
  32. static dispatch_once_t onceToken;
  33. dispatch_once(&onceToken, ^{
  34. auto cachesPath = standardPathNative(NSCachesDirectory);
  35. auto afterMacOsHomeDirPos = [cachesPath rangeOfString:@"Library/Developer"].location;
  36. NSCAssert(afterMacOsHomeDirPos != NSNotFound, @"simulator directory location is not under user's home directory: %@", cachesPath);
  37. applicationSupportPath = [[cachesPath substringToIndex:afterMacOsHomeDirPos] stringByAppendingPathComponent:@"Library/Application Support/vcmi"].stringByResolvingSymlinksInPath;
  38. });
  39. return applicationSupportPath.fileSystemRepresentation;
  40. }
  41. #endif
  42. const char *bundlePath() { return NSBundle.mainBundle.bundlePath.fileSystemRepresentation; }
  43. const char *frameworksPath() { return NSBundle.mainBundle.privateFrameworksPath.fileSystemRepresentation; }
  44. const char *bundleIdentifier() { return NSBundle.mainBundle.bundleIdentifier.UTF8String; }
  45. bool isOsVersionAtLeast(unsigned int osMajorVersion)
  46. {
  47. return NSProcessInfo.processInfo.operatingSystemVersion.majorVersion >= osMajorVersion;
  48. }
  49. void keepScreenOn(bool isEnabled)
  50. {
  51. UIApplication.sharedApplication.idleTimerDisabled = isEnabled ? YES : NO;
  52. }
  53. void shareFile(const std::string & filePath)
  54. {
  55. NSString *nsPath = [NSString stringWithUTF8String:filePath.c_str()];
  56. if (nsPath == nil)
  57. return;
  58. NSURL *url = [NSURL fileURLWithPath:nsPath];
  59. if (!url) return;
  60. NSArray *items = @[url];
  61. UIActivityViewController *controller = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:nil];
  62. UIViewController *root = UIApplication.sharedApplication.keyWindow.rootViewController;
  63. if (root.presentedViewController != nil)
  64. [root.presentedViewController presentViewController:controller animated:YES completion:nil];
  65. else
  66. [root presentViewController:controller animated:YES completion:nil];
  67. }
  68. std::string iphoneHardwareId()
  69. {
  70. struct utsname systemInfo;
  71. uname(&systemInfo);
  72. return std::string(systemInfo.machine);
  73. }
  74. }