Skip to content

Commit 821bc4f

Browse files
authored
Merge pull request #3778 from bbirman/window-notifications
Fix for snapshot dismissal
2 parents 2adad58 + 20ab72c commit 821bc4f

File tree

2 files changed

+41
-15
lines changed

2 files changed

+41
-15
lines changed

libs/SalesforceSDKCore/SalesforceSDKCore/Classes/Common/SalesforceSDKManager.m

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ - (instancetype)init {
279279
[[NSNotificationCenter defaultCenter] addObserver:self.sdkManagerFlow selector:@selector(handleAppBackground:) name:UIApplicationDidEnterBackgroundNotification object:nil];
280280
[[NSNotificationCenter defaultCenter] addObserver:self.sdkManagerFlow selector:@selector(handleAppTerminate:) name:UIApplicationWillTerminateNotification object:nil];
281281
[[NSNotificationCenter defaultCenter] addObserver:self.sdkManagerFlow selector:@selector(handleSceneWillEnterForeground:) name:UISceneWillEnterForegroundNotification object:nil];
282+
[[NSNotificationCenter defaultCenter] addObserver:self.sdkManagerFlow selector:@selector(handleSceneDidActivate:) name:UISceneDidActivateNotification object:nil];
282283
[[NSNotificationCenter defaultCenter] addObserver:self.sdkManagerFlow selector:@selector(handleSceneDidEnterBackground:) name:UISceneDidEnterBackgroundNotification object:nil];
283284
[[NSNotificationCenter defaultCenter] addObserver:self.sdkManagerFlow selector:@selector(handleSceneWillConnect:) name:UISceneWillConnectNotification object:nil];
284285
[[NSNotificationCenter defaultCenter] addObserver:self.sdkManagerFlow selector:@selector(handleSceneDidDisconnect:) name:UISceneDidDisconnectNotification object:nil];
@@ -595,20 +596,37 @@ - (void)handleAppBackground:(NSNotification *)notification
595596
- (void)handleAppTerminate:(NSNotification *)notification { }
596597

597598
- (void)handleSceneWillEnterForeground:(NSNotification *)notification {
598-
UIScene *scene = (UIScene *)notification.object;
599-
NSString *sceneId = scene.session.persistentIdentifier;
600-
[SFSDKCoreLogger d:[self class] format:@"Scene %@ is resuming active state.", sceneId];
599+
UIScene *scene = [self sceneFromNotification:notification];
600+
NSString *sceneId = scene.session.persistentIdentifier;
601+
[SFSDKCoreLogger d:[self class] format:@"Scene %@ is entering foreground.", sceneId];
601602

602-
@try {
603-
[self dismissSnapshot:scene completion:nil];
604-
}
605-
@catch (NSException *exception) {
606-
[SFSDKCoreLogger w:[self class] format:@"Exception thrown while removing security snapshot view for scene %@: '%@'. Will continue to resume scene.", sceneId, [exception reason]];
607-
}
603+
// Using this to dismiss snapshot for screen mirroring
604+
if (scene.session.role == UIWindowSceneSessionRoleExternalDisplayNonInteractive) {
605+
@try {
606+
[self dismissSnapshot:scene completion:nil];
607+
}
608+
609+
@catch (NSException *exception) {
610+
[SFSDKCoreLogger w:[self class] format:@"Exception thrown while removing security snapshot view for scene %@: '%@'. Will continue to resume scene.", sceneId, [exception reason]];
611+
}
612+
}
613+
}
614+
615+
- (void)handleSceneDidActivate:(NSNotification *)notification {
616+
UIScene *scene = [self sceneFromNotification:notification];
617+
NSString *sceneId = scene.session.persistentIdentifier;
618+
[SFSDKCoreLogger d:[self class] format:@"Scene %@ is resuming active state.", sceneId];
619+
620+
@try {
621+
[self dismissSnapshot:scene completion:nil];
622+
}
623+
@catch (NSException *exception) {
624+
[SFSDKCoreLogger w:[self class] format:@"Exception thrown while removing security snapshot view for scene %@: '%@'. Will continue to resume scene.", sceneId, [exception reason]];
625+
}
608626
}
609627

610628
- (void)handleSceneWillConnect:(NSNotification *)notification {
611-
UIScene *scene = (UIScene *)notification.object;
629+
UIScene *scene = [self sceneFromNotification:notification];
612630
if (scene.activationState == UISceneActivationStateBackground) {
613631
SFSDKWindowContainer *activeWindow = [[SFSDKWindowManager sharedManager] activeWindow:scene];
614632
if ([activeWindow isAuthWindow] || [activeWindow isScreenLockWindow]) {
@@ -619,7 +637,7 @@ - (void)handleSceneWillConnect:(NSNotification *)notification {
619637
}
620638

621639
- (void)handleSceneDidEnterBackground:(NSNotification *)notification {
622-
UIScene *scene = (UIScene *)notification.object;
640+
UIScene *scene = [self sceneFromNotification:notification];
623641
NSString *sceneId = scene.session.persistentIdentifier;
624642

625643
[SFSDKCoreLogger d:[self class] format:@"Scene %@ is entering background.", sceneId];
@@ -647,10 +665,18 @@ - (void)handleSceneDidEnterBackground:(NSNotification *)notification {
647665
}
648666

649667
- (void)handleSceneDidDisconnect:(NSNotification *)notification {
650-
UIScene *scene = (UIScene *)notification.object;
668+
UIScene *scene = [self sceneFromNotification:notification];
651669
[self.snapshotViewControllers removeObject:scene.session.persistentIdentifier];
652670
}
653671

672+
- (nullable UIScene *)sceneFromNotification:(NSNotification *)notification {
673+
id object = notification.object;
674+
if ([object isKindOfClass:[UIScene class]]) {
675+
return object;
676+
}
677+
return nil;
678+
}
679+
654680
- (void)handleAuthCompleted:(NSNotification *)notification { }
655681

656682
- (void)handleIDPInitiatedAuthCompleted:(NSNotification *)notification

libs/SalesforceSDKCore/SalesforceSDKCoreTests/SalesforceSDKManagerTests.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ - (void)testSnapshotRespondsToStateEvents
195195
UIScene *scene = UIApplication.sharedApplication.connectedScenes.allObjects.firstObject;
196196
[[NSNotificationCenter defaultCenter] postNotificationName:UISceneDidEnterBackgroundNotification object:scene];
197197
XCTAssertTrue(presentOnBackground, @"Did not respond to scene background.");
198-
[[NSNotificationCenter defaultCenter] postNotificationName:UISceneWillEnterForegroundNotification object:scene];
198+
[[NSNotificationCenter defaultCenter] postNotificationName:UISceneDidActivateNotification object:scene];
199199
XCTAssertTrue(dismissOnDidBecomeActive, @"Did not respond to app did become active.");
200200
}
201201

@@ -241,7 +241,7 @@ - (void)testDefaultSnapshotViewControllerIsProvided
241241
// This will simulate that the snapshot view is being presented
242242
UIView* fakeView = [UIView new];
243243
[fakeView addSubview:defaultViewControllerOnPresentation.view];
244-
[[NSNotificationCenter defaultCenter] postNotificationName:UISceneWillEnterForegroundNotification
244+
[[NSNotificationCenter defaultCenter] postNotificationName:UISceneDidActivateNotification
245245
object:scene];
246246
XCTAssertEqual(defaultViewControllerOnPresentation, defaultViewControllerOnDismissal, @"Default snapshot view controller on dismissal is different than the one provided on presentation!");
247247
}
@@ -288,7 +288,7 @@ - (void)testCustomSnapshotViewControllerIsUsed
288288
// This will simulate that the snapshot view is being presented
289289
UIView* fakeView = [UIView new];
290290
[fakeView addSubview:customSnapshot.view];
291-
[[NSNotificationCenter defaultCenter] postNotificationName:UISceneWillEnterForegroundNotification object:scene];
291+
[[NSNotificationCenter defaultCenter] postNotificationName:UISceneDidActivateNotification object:scene];
292292
XCTAssertEqual(customSnapshot, snapshotOnDismissal, @"Custom snapshot view controller was not used on dismissal!");
293293
}
294294

0 commit comments

Comments
 (0)