2 versions 1 works 1 doesn't. UIViewRepresentable to show a PDF
@AppStorage(DefaultsKey.userActiveBook.rawValue) var activeBook : URL = Bundle.main.url(forResource: "BookPlaceHolder", withExtension: "pdf")!
func makeUIView(context: Context) -> PDFView {
do {
let pdfData = try Data(contentsOf: activeBook)
pdfView.document = PDFDocument(data: pdfData) //<---- is nil
...
}
catch let Error {
print(Error)
}
}
fails with
Error Domain=NSCocoaErrorDomain Code=260 "The file “BookPlaceHolder.pdf” couldn’t be opened because there is no such file."
func makeUIView(context: Context) -> PDFView {
do {
let pdfData = try Data(contentsOf: Bundle.main.url(forResource: "BookPlaceHolder", withExtension: "pdf")!)
pdfView.document = PDFDocument(data: pdfData)
...
}
catch let Error {
print(Error)
}
}
Works perfectly. What is it with using @AppStorage to access the exact same URL causing the discrepancies ?