O3DEApplicationDelegate_iOS.mm 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <Launcher.h>
  9. #include <../Common/Apple/Launcher_Apple.h>
  10. #include <../Common/UnixLike/Launcher_UnixLike.h>
  11. #include <AzFramework/API/ApplicationAPI_Platform.h>
  12. #include <AzCore/IO/SystemFile.h> // for AZ_MAX_PATH_LEN
  13. #include <AzCore/Utils/SystemUtilsApple_Platform.h>
  14. #import <UIKit/UIKit.h>
  15. namespace
  16. {
  17. const char* GetAppWriteStoragePath()
  18. {
  19. static char pathToApplicationPersistentStorage[AZ::IO::MaxPathLength];
  20. // Unlike Mac where we have unrestricted access to the filesystem, iOS apps are sandboxed such
  21. // that you can only access a pre-defined set of directories.
  22. // https://developer.apple.com/library/mac/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html
  23. AZ::SystemUtilsApple::GetPathToUserApplicationSupportDirectory(AZStd::span(pathToApplicationPersistentStorage));
  24. return pathToApplicationPersistentStorage;
  25. }
  26. }
  27. @interface O3DEApplicationDelegate_iOS : NSObject<UIApplicationDelegate>
  28. {
  29. }
  30. @end // O3DEApplicationDelegate_iOS Interface
  31. @implementation O3DEApplicationDelegate_iOS
  32. - (int)runO3DEApplication
  33. {
  34. #if AZ_TESTS_ENABLED
  35. // TODO: iOS needs to determine how to get around being able to run in monolithic mode (ie no dynamic modules)
  36. return static_cast<int>(ReturnCode::ErrUnitTestNotSupported);
  37. #else
  38. using namespace O3DELauncher;
  39. PlatformMainInfo mainInfo;
  40. mainInfo.m_updateResourceLimits = IncreaseResourceLimits;
  41. mainInfo.m_appResourcesPath = GetAppResourcePath();
  42. mainInfo.m_appWriteStoragePath = GetAppWriteStoragePath();
  43. mainInfo.m_additionalVfsResolution = "\t- Check that usbmuxconnect is running and not reporting any errors when connecting to localhost";
  44. NSArray* commandLine = [[NSProcessInfo processInfo] arguments];
  45. for (size_t argIndex = 0; argIndex < [commandLine count]; ++argIndex)
  46. {
  47. NSString* arg = commandLine[argIndex];
  48. if (!mainInfo.AddArgument([arg UTF8String]))
  49. {
  50. return static_cast<int>(ReturnCode::ErrCommandLine);
  51. }
  52. }
  53. ReturnCode status = Run(mainInfo);
  54. return static_cast<int>(status);
  55. #endif // AZ_TESTS_ENABLED
  56. }
  57. - (void)launchO3DEApplication
  58. {
  59. const int exitCode = [self runO3DEApplication];
  60. exit(exitCode);
  61. }
  62. - (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
  63. {
  64. // prevent the o3de runtime from running when launched in a xctest environment, otherwise the
  65. // testing framework will kill the "app" due to the lengthy bootstrap process
  66. if ([[NSProcessInfo processInfo] environment][@"XCTestConfigurationFilePath"] == nil)
  67. {
  68. [self performSelector:@selector(launchO3DEApplication) withObject:nil afterDelay:0.0];
  69. }
  70. return YES;
  71. }
  72. - (void)applicationWillResignActive:(UIApplication*)application
  73. {
  74. AzFramework::IosLifecycleEvents::Bus::Broadcast(
  75. &AzFramework::IosLifecycleEvents::Bus::Events::OnWillResignActive);
  76. }
  77. - (void)applicationDidEnterBackground:(UIApplication*)application
  78. {
  79. AzFramework::IosLifecycleEvents::Bus::Broadcast(
  80. &AzFramework::IosLifecycleEvents::Bus::Events::OnDidEnterBackground);
  81. }
  82. - (void)applicationWillEnterForeground:(UIApplication*)application
  83. {
  84. AzFramework::IosLifecycleEvents::Bus::Broadcast(
  85. &AzFramework::IosLifecycleEvents::Bus::Events::OnWillEnterForeground);
  86. }
  87. - (void)applicationDidBecomeActive:(UIApplication*)application
  88. {
  89. AzFramework::IosLifecycleEvents::Bus::Broadcast(
  90. &AzFramework::IosLifecycleEvents::Bus::Events::OnDidBecomeActive);
  91. }
  92. - (void)applicationWillTerminate:(UIApplication *)application
  93. {
  94. AzFramework::IosLifecycleEvents::Bus::Broadcast(
  95. &AzFramework::IosLifecycleEvents::Bus::Events::OnWillTerminate);
  96. }
  97. - (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
  98. {
  99. AzFramework::IosLifecycleEvents::Bus::Broadcast(
  100. &AzFramework::IosLifecycleEvents::Bus::Events::OnDidReceiveMemoryWarning);
  101. }
  102. @end // O3DEApplicationDelegate_iOS Implementation