UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Attempting to use PKAddPassesViewController inside SwiftUI with a UIViewControllerRepresentable

Forums > SwiftUI

I am attempting to get PKAddPasses to work on SwiftUI and have exahusted all effort, I am starting to think its impossible, the code so far I am able to get the add pass screen to show up and when pressing "Add" the pass gets added to the Wallet however neither the "Add" or "Cancel" button make the view disappear.

struct PassKit: UIViewControllerRepresentable {

    var pass: PKPass

    func makeUIViewController(context: Context) -> PKAddPassesViewController {
        let passvc = PKAddPassesViewController(pass: self.pass)
        return passvc!
    }

    func updateUIViewController(_ uiViewController: PKAddPassesViewController, context: Context) {

    }
}

passing a PKPass to the pass varible successfully gets the screen to appear however my struggle is getting it to disappear i have tried this as a last ditch resort

struct PassKit: UIViewControllerRepresentable {

    @Binding var done: Bool
    var pass: PKPass

    class Coordinator: NSObject, PKAddPassesViewControllerDelegate {
        var parent: PassKit

        init(_ parent: PassKit) {
            self.parent = parent
        }

        func addPassesViewControllerDidFinish(_ controller: PKAddPassesViewController) {
            parent.done = false
        }
    }

    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }

    func makeUIViewController(context: Context) -> PKAddPassesViewController {
        let passvc = PKAddPassesViewController(pass: self.pass)
        passvc?.delegate = context.coordinator
        return passvc!
    }

    func updateUIViewController(_ uiViewController: PKAddPassesViewController, context: Context) {

    }

}

the idea would be to bind the done bool and when the its true it would trigger


                        NavigationLink(
                            destination:
                                PassKit(done: self.$PassKitDone.onChange({ done in
                                    self.PassKitPresent = false
                                }))
                                .ignoresSafeArea()
                                .navigationBarHidden(true),
                            isActive: $PassKitPresent,
                            label: {})

this swifui code to to go back on the navigation link.

Please can someone help really do need to get this working

3      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.