Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for snapshot dismissal #3778

Merged
merged 4 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
self.view.backgroundColor = [UIColor salesforceSystemBackgroundColor];
}

- (BOOL)shouldAutorotate {

Check warning on line 111 in libs/SalesforceSDKCore/SalesforceSDKCore/Classes/Common/SalesforceSDKManager.m

View workflow job for this annotation

GitHub Actions / ios-pr (SalesforceSDKCore) / test-ios

implementing deprecated method [-Wdeprecated-implementations]
return YES;
}

Expand Down Expand Up @@ -279,6 +279,7 @@
[[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];
Expand Down Expand Up @@ -595,20 +596,37 @@
- (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]) {
Expand All @@ -619,7 +637,7 @@
}

- (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];
Expand Down Expand Up @@ -647,10 +665,18 @@
}

- (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 {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this to check the object on the notification more safely

id object = notification.object;
if ([object isKindOfClass:[UIScene class]]) {
return object;
}
return nil;
}

- (void)handleAuthCompleted:(NSNotification *)notification { }

- (void)handleIDPInitiatedAuthCompleted:(NSNotification *)notification
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}

Expand Down Expand Up @@ -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!");
}
Expand Down Expand Up @@ -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!");
}

Expand Down
Loading