Skip to content

Move view controller helpers to Stripe Core. #4686

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

nschris-stripe
Copy link
Contributor

@nschris-stripe nschris-stripe commented Mar 19, 2025

Summary

This moves some ViewController helpers used for SwiftUI presentation into StripeCore. I am doing this because we are actively considering using a similar approach to the payment bottom sheet view modifier for the Connect SDK. There is one minor change I made to make findViewController more accurate, called out in a comment.

Demo

Simulator.Screen.Recording.-.iPhone.8.Plus.-.2025-03-19.at.12.32.52.mp4

@nschris-stripe nschris-stripe force-pushed the nschris/moveViewControllerHelpers branch 2 times, most recently from bba7804 to 13ad2e4 Compare March 19, 2025 16:42
@nschris-stripe nschris-stripe marked this pull request as ready for review March 19, 2025 19:25
@nschris-stripe nschris-stripe requested review from a team as code owners March 19, 2025 19:25
@nschris-stripe nschris-stripe force-pushed the nschris/moveViewControllerHelpers branch from 13ad2e4 to 1afd7dc Compare March 19, 2025 20:49

@_spi(STP) public extension UIResponder {
@available(iOSApplicationExtension, unavailable)
@objc func findViewController() -> UIViewController? {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Previously the function assumed the only things in the responder chain were UIViewController or UIView. That is not a requirement of the responder chain, so I made this slightly more generic.

Previously looked like this:

func findViewController(for uiView: UIView) -> UIViewController? {
    if let nextResponder = uiView.next as? UIViewController {
        return nextResponder
    } else if let nextResponder = uiView.next as? UIView {
        return findViewController(for: nextResponder)
    } else {
        // Can't find a view, attempt to grab the top most view controller
        return topMostViewController()
    }
}

porter-stripe
porter-stripe previously approved these changes Mar 19, 2025
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.

2 participants