Skip to content
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

Help on implementing with SwiftUI #5

Open
arbyruns opened this issue Sep 10, 2020 · 4 comments
Open

Help on implementing with SwiftUI #5

arbyruns opened this issue Sep 10, 2020 · 4 comments
Labels
enhancement New feature or request

Comments

@arbyruns
Copy link

I'm fairly new to programming, but as I understand there is no viewDidLoad with SwiftUI. Are there any walkthroughs on getting this setup for SwiftUI? Thanks!

@frboulais
Copy link
Member

Hello there! I'm working on the SwiftUI implementation of the framework, I'll keep you in touch when it is available! Thanks for your message :)

@frboulais frboulais added the enhancement New feature or request label Sep 16, 2020
@LualdiD
Copy link

LualdiD commented Oct 8, 2020

Any progress on this?

@thecoolwinter
Copy link

Temp Fix

This will only work for apps that use UIKit's AppDelegate and SceneDelegate

Configure your BMCManager.shared instance in application(_ application: UIApplication, didFinishLaunchingWithOptions like normal.

Then in SceneDelegate, in scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions), after you've configured your UIHostingView to be the window's root view controller, make it the manager's view controller.

Full code:

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        // Use a UIHostingController as window root view controller.
        if let windowScene = scene as? UIWindowScene {
            let window = UIWindow(windowScene: windowScene)
            let controller = UIHostingController(rootView: contentView)
            window.rootViewController = controller
            self.window = window
            window.makeKeyAndVisible()
        }
        
        // Make the window's root controller the presentingViewController
        BMCManager.shared.presentingViewController = window?.rootViewController
    }
}

Then anywhere in your SwiftUI code just call BMCManager.shared.start() and the BuyMeACoffee screen will be presented on top of the entire app.

@thecoolwinter
Copy link

thecoolwinter commented Oct 12, 2021

If you're using the SwiftUI lifecycle just add this on your App's init

BMCManager.shared.presentingViewController = UIApplication.shared.connectedScenes.filter { $0.activationState == .foregroundActive }.map { $0 as? UIWindowScene }.compactMap { $0 }.first?.windows.filter { $0.isKeyWindow }.first?.rootViewController

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants