From 32fa134e92fa8e544ba39e8e37a625dee3e3ed40 Mon Sep 17 00:00:00 2001 From: Brianna Birman Date: Sun, 24 Nov 2024 23:55:08 -0800 Subject: [PATCH 1/2] Fix for snapshot dismissal --- .../Classes/Common/SalesforceSDKManager.m | 50 ++++++++++++++----- 1 file changed, 38 insertions(+), 12 deletions(-) diff --git a/libs/SalesforceSDKCore/SalesforceSDKCore/Classes/Common/SalesforceSDKManager.m b/libs/SalesforceSDKCore/SalesforceSDKCore/Classes/Common/SalesforceSDKManager.m index bbc1bc752b..7571c02e94 100644 --- a/libs/SalesforceSDKCore/SalesforceSDKCore/Classes/Common/SalesforceSDKManager.m +++ b/libs/SalesforceSDKCore/SalesforceSDKCore/Classes/Common/SalesforceSDKManager.m @@ -279,6 +279,7 @@ - (instancetype)init { [[NSNotificationCenter defaultCenter] addObserver:self.sdkManagerFlow selector:@selector(handleAppBackground:) name:UIApplicationDidEnterBackgroundNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self.sdkManagerFlow selector:@selector(handleAppTerminate:) name:UIApplicationWillTerminateNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self.sdkManagerFlow selector:@selector(handleSceneWillEnterForeground:) name:UISceneWillEnterForegroundNotification object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:self.sdkManagerFlow selector:@selector(handleSceneDidActivate:) name:UISceneDidActivateNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self.sdkManagerFlow selector:@selector(handleSceneDidEnterBackground:) name:UISceneDidEnterBackgroundNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self.sdkManagerFlow selector:@selector(handleSceneWillConnect:) name:UISceneWillConnectNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self.sdkManagerFlow selector:@selector(handleSceneDidDisconnect:) name:UISceneDidDisconnectNotification object:nil]; @@ -595,20 +596,37 @@ - (void)handleAppBackground:(NSNotification *)notification - (void)handleAppTerminate:(NSNotification *)notification { } - (void)handleSceneWillEnterForeground:(NSNotification *)notification { - UIScene *scene = (UIScene *)notification.object; - NSString *sceneId = scene.session.persistentIdentifier; - [SFSDKCoreLogger d:[self class] format:@"Scene %@ is resuming active state.", sceneId]; + UIScene *scene = [self sceneFromNotification:notification]; + NSString *sceneId = scene.session.persistentIdentifier; + [SFSDKCoreLogger d:[self class] format:@"Scene %@ is entering foreground.", sceneId]; - @try { - [self dismissSnapshot:scene completion:nil]; - } - @catch (NSException *exception) { - [SFSDKCoreLogger w:[self class] format:@"Exception thrown while removing security snapshot view for scene %@: '%@'. Will continue to resume scene.", sceneId, [exception reason]]; - } + // Using this to dismiss snapshot for screen mirroring + if (scene.session.role == UIWindowSceneSessionRoleExternalDisplayNonInteractive) { + @try { + [self dismissSnapshot:scene completion:nil]; + } + + @catch (NSException *exception) { + [SFSDKCoreLogger w:[self class] format:@"Exception thrown while removing security snapshot view for scene %@: '%@'. Will continue to resume scene.", sceneId, [exception reason]]; + } + } +} + +- (void)handleSceneDidActivate:(NSNotification *)notification { + UIScene *scene = [self sceneFromNotification:notification]; + NSString *sceneId = scene.session.persistentIdentifier; + [SFSDKCoreLogger d:[self class] format:@"Scene %@ is resuming active state.", sceneId]; + + @try { + [self dismissSnapshot:scene completion:nil]; + } + @catch (NSException *exception) { + [SFSDKCoreLogger w:[self class] format:@"Exception thrown while removing security snapshot view for scene %@: '%@'. Will continue to resume scene.", sceneId, [exception reason]]; + } } - (void)handleSceneWillConnect:(NSNotification *)notification { - UIScene *scene = (UIScene *)notification.object; + UIScene *scene = [self sceneFromNotification:notification]; if (scene.activationState == UISceneActivationStateBackground) { SFSDKWindowContainer *activeWindow = [[SFSDKWindowManager sharedManager] activeWindow:scene]; if ([activeWindow isAuthWindow] || [activeWindow isScreenLockWindow]) { @@ -619,7 +637,7 @@ - (void)handleSceneWillConnect:(NSNotification *)notification { } - (void)handleSceneDidEnterBackground:(NSNotification *)notification { - UIScene *scene = (UIScene *)notification.object; + UIScene *scene = [self sceneFromNotification:notification]; NSString *sceneId = scene.session.persistentIdentifier; [SFSDKCoreLogger d:[self class] format:@"Scene %@ is entering background.", sceneId]; @@ -647,10 +665,18 @@ - (void)handleSceneDidEnterBackground:(NSNotification *)notification { } - (void)handleSceneDidDisconnect:(NSNotification *)notification { - UIScene *scene = (UIScene *)notification.object; + UIScene *scene = [self sceneFromNotification:notification]; [self.snapshotViewControllers removeObject:scene.session.persistentIdentifier]; } +- (nullable UIScene *)sceneFromNotification:(NSNotification *)notification { + id object = notification.object; + if ([object isKindOfClass:[UIScene class]]) { + return object; + } + return nil; +} + - (void)handleAuthCompleted:(NSNotification *)notification { } - (void)handleIDPInitiatedAuthCompleted:(NSNotification *)notification From 20ab72c76dd86d11e891df26c0687e1622bb8cbb Mon Sep 17 00:00:00 2001 From: Brianna Birman Date: Mon, 25 Nov 2024 13:48:08 -0800 Subject: [PATCH 2/2] Update tests --- .../SalesforceSDKCoreTests/SalesforceSDKManagerTests.m | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libs/SalesforceSDKCore/SalesforceSDKCoreTests/SalesforceSDKManagerTests.m b/libs/SalesforceSDKCore/SalesforceSDKCoreTests/SalesforceSDKManagerTests.m index a14d965b3f..b4caa208f5 100644 --- a/libs/SalesforceSDKCore/SalesforceSDKCoreTests/SalesforceSDKManagerTests.m +++ b/libs/SalesforceSDKCore/SalesforceSDKCoreTests/SalesforceSDKManagerTests.m @@ -195,7 +195,7 @@ - (void)testSnapshotRespondsToStateEvents UIScene *scene = UIApplication.sharedApplication.connectedScenes.allObjects.firstObject; [[NSNotificationCenter defaultCenter] postNotificationName:UISceneDidEnterBackgroundNotification object:scene]; XCTAssertTrue(presentOnBackground, @"Did not respond to scene background."); - [[NSNotificationCenter defaultCenter] postNotificationName:UISceneWillEnterForegroundNotification object:scene]; + [[NSNotificationCenter defaultCenter] postNotificationName:UISceneDidActivateNotification object:scene]; XCTAssertTrue(dismissOnDidBecomeActive, @"Did not respond to app did become active."); } @@ -241,7 +241,7 @@ - (void)testDefaultSnapshotViewControllerIsProvided // This will simulate that the snapshot view is being presented UIView* fakeView = [UIView new]; [fakeView addSubview:defaultViewControllerOnPresentation.view]; - [[NSNotificationCenter defaultCenter] postNotificationName:UISceneWillEnterForegroundNotification + [[NSNotificationCenter defaultCenter] postNotificationName:UISceneDidActivateNotification object:scene]; XCTAssertEqual(defaultViewControllerOnPresentation, defaultViewControllerOnDismissal, @"Default snapshot view controller on dismissal is different than the one provided on presentation!"); } @@ -288,7 +288,7 @@ - (void)testCustomSnapshotViewControllerIsUsed // This will simulate that the snapshot view is being presented UIView* fakeView = [UIView new]; [fakeView addSubview:customSnapshot.view]; - [[NSNotificationCenter defaultCenter] postNotificationName:UISceneWillEnterForegroundNotification object:scene]; + [[NSNotificationCenter defaultCenter] postNotificationName:UISceneDidActivateNotification object:scene]; XCTAssertEqual(customSnapshot, snapshotOnDismissal, @"Custom snapshot view controller was not used on dismissal!"); }