I'm looking for a solution to view and edit PDF files in a SwiftUI app. Above all, the functionality should be given to sign the PDF/ to draw on it. Is there any way to do this? I'm new to Swift development so I'm learning SwiftUI / UIKit at the same time. As a result, I have a hard time writing a wrapper for UIKit code in SwiftUI. My Code can display a PDF but cannot edit it so far:
import Foundation
import SwiftUI
import UIKit
import PDFKit
struct PDFKitView: View {
var url: URL
var body: some View {
PDFKitRepresentedView(url)
}
}
struct PDFKitRepresentedView: UIViewRepresentable {
let url: URL
init(_ url: URL) {
self.url = url
}
func makeUIView(context: UIViewRepresentableContext<PDFKitRepresentedView>) -> PDFKitRepresentedView.UIViewType {
let pdfView = PDFView()
pdfView.document = PDFDocument(url: self.url)
pdfView.autoScales = true
return pdfView
}
func updateUIView(_ uiView: UIView, context: UIViewRepresentableContext<PDFKitRepresentedView>) {
// Update the view.
}
}
I already found a solution. I used the PDFFreedraw Repo from ClassicalDude on GitHub. This is a well-implemented open source solution. I was then able to write a wrapper for swift UI like this.
https://github.com/ClassicalDude/pdfView-Freedraw
import Foundation
import SwiftUI
import UIKit
import PDFKit
struct PDFKitEditorView: View {
var url: URL
var body: some View {
PDFKitEditorRepresentedView(url)
.ignoresSafeArea(.all)
.navigationBarHidden(true)
}
}
struct PDFKitEditorRepresentedView: UIViewControllerRepresentable {
let url: URL
init(_ url: URL) {
self.url = url
}
func makeUIViewController(context: Context) -> UIViewController {
let storyboard = UIStoryboard(name: "MainPDFView", bundle: Bundle.main)
let controller = storyboard.instantiateViewController(withIdentifier: "PDFViewControllerID") as? PDFViewController
controller!.url = self.url
return controller!
}
func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
}
}