diff --git a/ios/samples/gltf-viewer/gltf-viewer/FILViewController.mm b/ios/samples/gltf-viewer/gltf-viewer/FILViewController.mm index 5366a7ef44..80898ec19e 100644 --- a/ios/samples/gltf-viewer/gltf-viewer/FILViewController.mm +++ b/ios/samples/gltf-viewer/gltf-viewer/FILViewController.mm @@ -47,6 +47,9 @@ const float kToastDelayDuration = 2.0f; - (void)createRenderables; - (void)createLights; +- (void)appWillResignActive:(NSNotification*)notification; +- (void)appDidBecomeActive:(NSNotification*)notification; + @end @implementation FILViewController { @@ -73,6 +76,16 @@ const float kToastDelayDuration = 2.0f; self.title = @"https://google.github.io/filament/remote"; + // Observe lifecycle notifications to prevent us from rendering in the background. + [NSNotificationCenter.defaultCenter addObserver:self + selector:@selector(appWillResignActive:) + name:UIApplicationWillResignActiveNotification + object:nil]; + [NSNotificationCenter.defaultCenter addObserver:self + selector:@selector(appDidBecomeActive:) + name:UIApplicationDidBecomeActiveNotification + object:nil]; + // Arguments: // --model // path to glb or gltf file to load from documents directory @@ -125,6 +138,14 @@ const float kToastDelayDuration = 2.0f; [self.view addSubview:_toastLabel]; } +- (void)appWillResignActive:(NSNotification*)notification { + [self stopDisplayLink]; +} + +- (void)appDidBecomeActive:(NSNotification*)notification { + [self startDisplayLink]; +} + - (void)viewWillAppear:(BOOL)animated { [self startDisplayLink]; } @@ -330,6 +351,7 @@ const float kToastDelayDuration = 2.0f; } - (void)dealloc { + [NSNotificationCenter.defaultCenter removeObserver:self]; delete _server; delete _automation; self.modelView.engine->destroy(_indirectLight);