I’ve a Ionic Angular utility which makes use of the capacitor bridge to work together with iOS native.
I’ve created a customized plugin which sends file paths to the native facet in order that I can merge the information after which open the brand new file.
At the moment nonetheless the code runs up till I attempt to open the file. I’m pretty new to Swift and iOS programming so any assist can be appreciated.
Right here is the code:
import Capacitor
import PDFKit
@objc(GeneratePdfPlugin)
public class GeneratePdfPlugin: CAPPlugin, UIDocumentInteractionControllerDelegate {
@objc func generatePdfNative(_ name: CAPPluginCall) {
let worth = name.getString("worth") ?? ""
if let jsonData = worth.knowledge(utilizing: .utf8),
let jsonArray = strive? JSONSerialization.jsonObject(with: jsonData, choices: []) as? [String] {
if let outputURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first?.appendingPathComponent("merged.pdf") {
var inputURLs: [URL] = []
for fullPath in jsonArray {
print(fullPath)
inputURLs.append(URL(fileURLWithPath: fullPath));
}
mergePDFs(inputURLs: inputURLs, outputURL: outputURL)
let fileManager = FileManager.default
guard let outputDirectory = strive? fileManager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false) else {
name.reject("Did not entry the paperwork listing")
return
}
let outputURL = outputDirectory.appendingPathComponent("merged.pdf")
DispatchQueue.predominant.async {
let documentController = UIDocumentInteractionController(url: outputURL)
documentController.delegate = self
documentController.presentPreview(animated: true)
}
} else {
print("Couldn't create the output file URL.")
}
} else {
print("Invalid JSON format")
}
}
func mergePDFs(inputURLs: [URL], outputURL: URL) {
let pdfDocument = PDFDocument()
for inputURL in inputURLs {
if let doc = PDFDocument(url: inputURL) {
for pageIndex in 0 ..< doc.pageCount {
if let web page = doc.web page(at: pageIndex) {
pdfDocument.insert(web page, at: pdfDocument.pageCount)
}
}
}
}
pdfDocument.write(to: outputURL)
}
}
right here is the error:
ERROR: UIDocumentInteractionController delegate should implement documentInteractionControllerViewControllerForPreview: to permit preview
Cheers