I'm having great difficulty working out how to implement a PDFView using UIViewRepresentable.
I understand that I can get variables from SwiftUI -> UIKit via a simple Binding.
I understand that I need to use a Coordinator to detect PDFView Notifications such as PDFViewPageChanged etc and then pass data back with the Binding.
I cant find an example of how you can use Coordinator to pick up notifications. Any help would be greatly appretiated.
Thanks
EDIT
What I've tried - unfortuantly I never recieve a notification
func makeUIView(context: Context) -> PDFView {
let pdfView = PDFView()
pdfView.document = pdfDocument
pdfView.autoScales = true
NotificationCenter.default.publisher(for: .PDFViewPageChanged)
.map {$0.object as? PDFView}
.sink { (view) in
print("notification received")
}
return pdfView
}
Something like this:
struct SPDFView: UIViewRepresentable {
let document: PDFDocument
@Binding var selection: String?
func makeUIView(context: Context) -> PDFView {
let view = PDFView()
view.document = document
NotificationCenter.default.publisher(for: .PDFViewSelectionChanged)
.map { $0.object as? PDFView }
.sink { (view) in
// or do something else
self.selection = view?.currentSelection?.string
}
.store(in: &cancellables)
// or, if you don't want to use Combine:
// NotificationCenter.default.addObserver(forName: .PDFViewSelectionChanged, object: nil, queue: nil) { (notification) in
// guard let pdfView = notification.object as? PDFView else { return }
// self.selection = pdfView.currentSelection?.string
// }
return view
}
func updateUIView(_ uiView: PDFView, context: Context) {}
}