

**実現したいこと**  
Firebaseのstorageに保存しているPDFデータをswiftUIで表示する。  
**問題**  
Firebaseの通信やPDFDocumentは作成できるのに、PDFが画面に表示されない。

[code]

    import Firebase
    import FirebaseStorage
    import PDFKit
    
    class PDFInfo_Mathmatics: ObservableObject {
        @Published var pageNo: Int = 1
        @Published var pdfView: PDFView = PDFView()
        @Published var stateTopButton: Bool = false
        
        func addObserver() {
            NotificationCenter.default.addObserver(self, selector: #selector(self.pageChanged(_:)), name: Notification.Name.PDFViewPageChanged, object: nil)
        }
        
        @objc func pageChanged(_ notification: Notification) {
            pageNo = pdfView.currentPage!.pageRef!.pageNumber
            stateTopButton = pdfView.canGoToFirstPage
            print(self.pageNo)
            print("page is changed")
        }
        
    }
    
    struct ShowPDFView: View {
        @ObservedObject var pdfInfo: PDFInfo_Mathmatics
        
        var body: some View {
            PDFViewer(pdfInfo: pdfInfo)
        }
    }
    
    struct PDFViewer: UIViewRepresentable {
        @ObservedObject var pdfInfo: PDFInfo_Mathmatics
        @ObservedObject var Firebase = firebase()
        @ObservedObject var pdfDocument = downloadImage()
        
        var url: URL {
            return Firebase.url ?? Bundle.main.url(forResource: "SampleData2", withExtension: "pdf")!
        }
        
        func makeUIView(context: UIViewRepresentableContext<PDFViewer>) -> PDFViewer.UIViewType {
            pdfInfo.pdfView.autoScales = true
            pdfInfo.pdfView.usePageViewController(true)
            pdfInfo.pdfView.displayDirection = .vertical
            pdfDocument.PDFdownload()
            pdfInfo.pdfView.document = pdfDocument.pdfDate
            return pdfInfo.pdfView
        }
        
        func updateUIView(_ uiView: UIView, context: UIViewRepresentableContext<PDFViewer>) {
        }
        
    }
    
    struct PdfInfoView: View {
        @ObservedObject var pdfInfo: PDFInfo_Mathmatics
        @State var enableTopButton: Bool = true
        
        var body: some View {
            HStack{
                Text(String(pdfInfo.pageNo))
                // ボタンの状態を変えてクリック無効にする場合はこちら
                Button(action: {
                    pdfInfo.pdfView.goToFirstPage(self)
                }, label: {
                    Text("TOP")
                })
                .disabled(!pdfInfo.stateTopButton)
            }
        }
    }
    
    
    class downloadImage: ObservableObject{
        let PDFRef = Storage.storage().reference().child("MathmaticsSampleData/SampleData.pdf")
    
        @Published var pdfDate: PDFDocument = PDFDocument()
        
        func PDFdownload() {
            PDFRef.getData(maxSize: 82858620){date , error in
                if error != nil{
                    print("error")
                }else{
                    self.pdfDate = PDFDocument(data: date!)!
                }
            }
            
        }
    }
    
    struct PDFTests6: View {
        
        @ObservedObject var object = downloadImage()
        @ObservedObject var pdfInfo: PDFInfo_Mathmatics = PDFInfo_Mathmatics()
        
        var body: some View {
            VStack {
                ShowPDFView(pdfInfo: pdfInfo)
                PdfInfoView(pdfInfo: pdfInfo)
                .padding()
                Button("Tap me!"){
                    self.object.PDFdownload()
                }
            }
            .onAppear(){
                pdfInfo.addObserver()
            }
        }
    }
    
    struct PDFTests6_Previews: PreviewProvider {
        static var previews: some View {
            PDFTests6()
        }
    }
    
[/code]


CC BY-SA 4.0
2021-08-04T08:40:07.373
80595
2021-08-04T08:40:07.373
43783
question
firebase swiftui
SwiftUI,Firebaseを連携したPDFの表示