Я начинаю использовать PDFKit с файлом PDF, расположенным в корне, он работает со следующим кодом:
if let path = Bundle.main.path(forResource: "mypdf1", ofType: "pdf") {
let url = URL(fileURLWithPath: path)
if let pdfDocument = PDFDocument(url: url) {
pdfView.autoScales = true
pdfView.displayMode = .singlePageContinuous
pdfView.displayDirection = .vertical
pdfView.document = pdfDocument
print(path)
}
}
Но если я изменю файл PDF в каталоге, например, "mydirectory", это не работает, мой код выглядит следующим образом:
if let path = Bundle.main.path(forResource: "mypdf1", ofType: "pdf", inDirectory: "mydirectory") {
let url = URL(fileURLWithPath: path)
if let pdfDocument = PDFDocument(url: url) {
pdfView.autoScales = true
pdfView.displayMode = .singlePageContinuous
pdfView.displayDirection = .vertical
pdfView.document = pdfDocument
}
}
Любые предложения по решению проблемы пути.
ОБНОВЛЕНИЕ
Согласно предложению, попробуйте следующий код, но я также не могу визуализировать PDF.
if let documentURL = Bundle.main.url(forResource: "mypdf1", withExtension: "pdf", subdirectory: "mydirectory") {
if let document = PDFDocument(url: documentURL) {
pdfView.autoScales = true
pdfView.backgroundColor = UIColor.lightGray
pdfView.document = document
}
}