Skip to content

BugFix - Unable to present when a VC is already presented#43

Open
hassaan4584 wants to merge 6 commits intoformbricks:mainfrom
hassaan4584:main
Open

BugFix - Unable to present when a VC is already presented#43
hassaan4584 wants to merge 6 commits intoformbricks:mainfrom
hassaan4584:main

Conversation

@hassaan4584
Copy link
Copy Markdown

  • Issue fixed where the survey would not be presented when there is a ViewController already presented

@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Apr 1, 2026

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
1 out of 3 committers have signed the CLA.

✅ hassaan4584
❌ narjes
❌ narjes-abs


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.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 1, 2026

Walkthrough

This 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)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main bugfix: handling survey presentation when a ViewController is already presented, which aligns with the changes in PresentSurveyManager.swift.
Description check ✅ Passed The description directly relates to the changeset, explaining that it fixes an issue with survey presentation when a ViewController is already presented, which matches the implementation changes.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ 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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

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

📥 Commits

Reviewing files that changed from the base of the PR and between 52a3175 and b7a6beb.

📒 Files selected for processing (2)
  • FormbricksSDK.podspec
  • Sources/FormbricksSDK/Manager/PresentSurveyManager.swift

Comment on lines +20 to +25
if let navigation = viewController as? UINavigationController {
return topViewController(from: navigation.visibleViewController ?? navigation)
}
if let tabBar = viewController as? UITabBarController {
return topViewController(from: tabBar.selectedViewController ?? tabBar)
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical

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.

Suggested change
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants