BugFix - Unable to present when a VC is already presented#43
BugFix - Unable to present when a VC is already presented#43hassaan4584 wants to merge 6 commits intoformbricks:mainfrom
Conversation
hassaan4584
commented
Apr 1, 2026
- Issue fixed where the survey would not be presented when there is a ViewController already presented
…pMostVC_for_presenter IOS-6487 Add topMostVC for presenter
…iewcontroller already presented
|
narjes seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account. You have signed the CLA already but the status is still pending? Let us recheck it. |
WalkthroughThis pull request updates FormbricksSDK to version 1.2.0 and refactors the survey presentation logic. The version bump updates the CocoaPods podspec and corresponding git tag reference. PresentSurveyManager.swift's present method now validates the key window and root view controller availability before proceeding. It introduces a helper method to traverse the view controller hierarchy and find the topmost controller for presentation, handles cases where controllers are already presenting, and implements guards to prevent presenting over existing presentations. The presenter is now dynamically selected based on view controller state rather than always using the root controller. 🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@Sources/FormbricksSDK/Manager/PresentSurveyManager.swift`:
- Around line 20-25: The helper topViewController(from:) can recurse infinitely
when a UINavigationController has visibleViewController == nil or a
UITabBarController has selectedViewController == nil because the current code
falls back to passing the container (navigation/tabBar) back into
topViewController; fix by checking for the child first (use guard/if let) and if
nil return the container itself (or its top-most child if appropriate) instead
of calling topViewController with the same container; update the branches that
reference UINavigationController.visibleViewController and
UITabBarController.selectedViewController to unwrap into a local
childViewController and only recurse when that child is non-nil.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: ASSERTIVE
Plan: Pro
Run ID: 49fd7df7-0481-450f-b908-4be857ff926d
📒 Files selected for processing (2)
FormbricksSDK.podspecSources/FormbricksSDK/Manager/PresentSurveyManager.swift
| if let navigation = viewController as? UINavigationController { | ||
| return topViewController(from: navigation.visibleViewController ?? navigation) | ||
| } | ||
| if let tabBar = viewController as? UITabBarController { | ||
| return topViewController(from: tabBar.selectedViewController ?? tabBar) | ||
| } |
There was a problem hiding this comment.
Infinite recursion when visibleViewController or selectedViewController is nil.
If a UINavigationController has no view controllers in its stack (visibleViewController == nil), or a UITabBarController has no tabs (selectedViewController == nil), the fallback ?? navigation / ?? tabBar passes the same container back to topViewController, causing infinite recursion and a stack overflow.
🐛 Proposed fix to prevent infinite recursion
private func topViewController(from viewController: UIViewController) -> UIViewController {
if let presented = viewController.presentedViewController {
return topViewController(from: presented)
}
- if let navigation = viewController as? UINavigationController {
- return topViewController(from: navigation.visibleViewController ?? navigation)
+ if let navigation = viewController as? UINavigationController,
+ let visible = navigation.visibleViewController {
+ return topViewController(from: visible)
}
- if let tabBar = viewController as? UITabBarController {
- return topViewController(from: tabBar.selectedViewController ?? tabBar)
+ if let tabBar = viewController as? UITabBarController,
+ let selected = tabBar.selectedViewController {
+ return topViewController(from: selected)
}
return viewController
}📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| if let navigation = viewController as? UINavigationController { | |
| return topViewController(from: navigation.visibleViewController ?? navigation) | |
| } | |
| if let tabBar = viewController as? UITabBarController { | |
| return topViewController(from: tabBar.selectedViewController ?? tabBar) | |
| } | |
| private func topViewController(from viewController: UIViewController) -> UIViewController { | |
| if let presented = viewController.presentedViewController { | |
| return topViewController(from: presented) | |
| } | |
| if let navigation = viewController as? UINavigationController, | |
| let visible = navigation.visibleViewController { | |
| return topViewController(from: visible) | |
| } | |
| if let tabBar = viewController as? UITabBarController, | |
| let selected = tabBar.selectedViewController { | |
| return topViewController(from: selected) | |
| } | |
| return viewController | |
| } |
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@Sources/FormbricksSDK/Manager/PresentSurveyManager.swift` around lines 20 -
25, The helper topViewController(from:) can recurse infinitely when a
UINavigationController has visibleViewController == nil or a UITabBarController
has selectedViewController == nil because the current code falls back to passing
the container (navigation/tabBar) back into topViewController; fix by checking
for the child first (use guard/if let) and if nil return the container itself
(or its top-most child if appropriate) instead of calling topViewController with
the same container; update the branches that reference
UINavigationController.visibleViewController and
UITabBarController.selectedViewController to unwrap into a local
childViewController and only recurse when that child is non-nil.