-
-
Notifications
You must be signed in to change notification settings - Fork 86
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: make getTopWindow
more robust
#621
fix: make getTopWindow
more robust
#621
Conversation
d141c25
to
7b919e7
Compare
ios/extensions/UIWindow.swift
Outdated
if #available(iOS 13.0, *) { | ||
for scene in UIApplication.shared.connectedScenes { | ||
if scene.activationState == .foregroundActive, | ||
let windowScene = scene as? UIWindowScene, | ||
let keyWindow = windowScene.windows.first(where: { $0.isKeyWindow }) { | ||
return keyWindow | ||
} | ||
} | ||
return nil | ||
} else { | ||
return UIApplication.shared.windows.last { $0.isKeyWindow } | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The changes looks good to me, but may I ask you to slightly re-work this part?
I would like to have this code somewhere isolated, because potentially we may want to get an access to main window in future, so I would move this code to an extension:
extension UIApplication {
var activeWindow: UIWindow? {
if #available(iOS 13.0, *) {
for scene in connectedScenes {
if scene.activationState == .foregroundActive,
let windowScene = scene as? UIWindowScene,
let keyWindow = windowScene.windows.first(where: { $0.isKeyWindow }) {
return keyWindow
}
}
return nil
} else {
return windows.last { $0.isKeyWindow }
}
}
}
And then use it like:
return keyboardWindow ?? UIApplication.shared.activeWindow
Not sure if this code works - just wanted to show the idea. Hope you can test these modifications in your project 👀
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will try and let you know!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kirillzyusko Looks like it functionally works the same, but I found a new issue that I'm not sure if is the result of this PR, or also present in the existing code.
In my app, I have routes guarded behind an auth check, and the Screens
rendered in the Navigator
change based on the auth state. When I open the OverKeyboardView
for the first time, it works, and navigation events such as push
can be done and OverKeyboardView
still works. But if the user logs out, the Screens
are remounted and the next time I try to open this OverKeyboardView
, the children become invisible again.
Do you think this PR should be put on hold until I figure out the fix for that as well?
EDIT: I would like to know if you can reproduce this as well, by adding more complicated routes to the example if you have the time 🙏
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, sad to hear that it still has issues 😔
Would you mind to submit a new issues and describe hte problem? Just want to track it somewhere so that I know that I'll not forget about the problem and will return to it :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kirillzyusko let me create an issue when I get some time this week! Thanks man
And yeah, if possible, please fix I think running |
7b919e7
to
e2f7733
Compare
return windows.last { $0.isKeyWindow } | ||
} | ||
} | ||
|
||
static func topViewController( | ||
base: UIViewController? = UIApplication.shared.keyWindow?.rootViewController |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kirillzyusko Xcode also complains about using UIApplication.shared.keyWindow
, I haven't tested it yet but replacing this with the activeWindow
extension removes the warning and should achieve the same thing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes @thespacemanatee I'm also constantly getting these warnings 🙈
I would split changes in two parts anyway 🙃 Like merge these changes and then open a second PR that will repace a single line.
In this case it'll be easier to manage 😊
Merged @thespacemanatee 🙌 Would you mind to submit a PR that we discussed here? |
## 📜 Description Replaced the usage of deprecated `keyWindow` with our `activeWindow` extension (which is not depecated). ## 💡 Motivation and Context We shouldn't rely on deprecated syntax. In #621 we added `.activeWindow` extension which represents a better alternative to `.keyWindow`. So I thought it would be better to not use deprecated syntax anymore and use new extension. ## 📢 Changelog <!-- High level overview of important changes --> <!-- For example: fixed status bar manipulation; added new types declarations; --> <!-- If your changes don't affect one of platform/language below - then remove this platform/language --> ### iOS - replace `.keyWindow` with `.activeWindow`; ## 🤔 How Has This Been Tested? Tested manually on CI (e2e tests). ## 📸 Screenshots (if appropriate): |Before|After| |------|------| |<img width="1709" alt="image" src="https://github.com/user-attachments/assets/5e7bfe97-ad64-4871-8d0e-3405034f1eb6">|<img width="1741" alt="image" src="https://github.com/user-attachments/assets/298c1e4e-d499-48a3-990d-2c1c0e21b699">| ## 📝 Checklist - [x] CI successfully passed - [x] I added new mocks and corresponding unit-tests if library API was changed
📜 Description
This PR updates the top window retrieval method:
Old:
UIApplication.shared.windows.last
(deprecated in iOS 15+)New:
UIWindowScene
-based approachDifferences:
UIApplication.shared.windows.last
:UIWindowScene
:UIWindowScenes
💡 Motivation and Context
Closes #618
In our case, this change was required to get the children of the
OverKeyboardView
to show. Without it, they were still rendered, but invisible. This change should enhance the reliability across different iOS versions and device types.📢 Changelog
iOS
UIWindowScene
as opposed to the last window of all windows associated with the app🤔 How Has This Been Tested?
It works on my company project. Did not manage to get the example app to compile on my machine. Could be due to the (lack of) provisioning profiles.
📝 Checklist