Skip to content

Commit 7e5a18c

Browse files
committed
update deprecated lastobject usage to retrieve active window scene directly
previous approach seems to get all windows, find the last one, then get its window scene, but we can use `UISceneActivationStateForegroundActive` to find this directly
1 parent dbc222d commit 7e5a18c

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

Modules/Sources/WPMediaPicker/WPMediaCapturePreviewCollectionView.m

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ - (void)startCapture
110110
CALayer *viewLayer = self.previewView.layer;
111111
self.captureVideoPreviewLayer.frame = viewLayer.bounds;
112112
self.captureVideoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
113-
UIWindowScene *currentScene = [[[[UIApplication sharedApplication] windows] lastObject] windowScene];
113+
UIWindowScene *currentScene = [self activeWindowScene];
114114
self.captureVideoPreviewLayer.connection.videoRotationAngle = [self videoRotationAngleForInterfaceOrientation:[currentScene interfaceOrientation]];
115115
[viewLayer addSublayer:self.captureVideoPreviewLayer];
116116
});
@@ -121,10 +121,26 @@ - (void)startCapture
121121

122122
- (void)deviceOrientationDidChange:(NSNotification *)notification
123123
{
124-
UIWindowScene *currentScene = [[[[UIApplication sharedApplication] windows] lastObject] windowScene];
124+
UIWindowScene *currentScene = [self activeWindowScene];
125125
self.captureVideoPreviewLayer.connection.videoRotationAngle = [self videoRotationAngleForInterfaceOrientation:[currentScene interfaceOrientation]];
126126
}
127127

128+
- (UIWindowScene *)activeWindowScene
129+
{
130+
for (UIScene *scene in [UIApplication sharedApplication].connectedScenes) {
131+
if ([scene isKindOfClass:[UIWindowScene class]] && scene.activationState == UISceneActivationStateForegroundActive) {
132+
return (UIWindowScene *)scene;
133+
}
134+
}
135+
// Fallback to first window scene if no active one found
136+
for (UIScene *scene in [UIApplication sharedApplication].connectedScenes) {
137+
if ([scene isKindOfClass:[UIWindowScene class]]) {
138+
return (UIWindowScene *)scene;
139+
}
140+
}
141+
return nil;
142+
}
143+
128144
- (CGFloat)videoRotationAngleForInterfaceOrientation:(UIInterfaceOrientation)orientation
129145
{
130146
switch (orientation) {

0 commit comments

Comments
 (0)