0

I am attempting to get PKAddPasses to work on SwiftUI. With my code provided, I am able to get the add pass screen to show up. When pressing "Add", the pass gets added to the Wallet. However, neither the "Add" nor the "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 variable successfully gets the screen to appear. However, I can not make it disappear and 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 boolean and when the its true, it would trigger a disappearing action.

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

This is the SwiftUI code to to go back to the navigation link.

Roland Lariotte
  • 2,606
  • 1
  • 16
  • 40

1 Answers1

0

Your struct is correct. I modified it a little bit to look like the following:

import Foundation
import PassKit
import SwiftUI
import UIKit


struct AddPassView: UIViewControllerRepresentable {
    
    typealias UIViewControllerType = PKAddPassesViewController
    
    @Environment(\.presentationMode) var presentationMode
    
    @Binding var pass: PKPass?
    
    func makeUIViewController(context: Context) -> PKAddPassesViewController {
        let passVC = PKAddPassesViewController(pass: self.pass!)
        return passVC!
    }
    
    func updateUIViewController(_ uiViewController: PKAddPassesViewController, context: Context) {
        // Nothing goes here
    }
}

Now your issue is actually with the NavigationLink. Use a sheet instead like this:

content
    .sheet(isPresented: self.$passViewControllerVisible){
           AddPassView(pass: self.$newPass)
     }

And then the "Add" and "Cancel" will work on their own.

Your code actually helped me put together the working struct, I just provided the sheet part. So thank you!

Nawaf
  • 149
  • 1
  • 9