Using PDFKit to Open PDF in App Automatically after Downloading

453 views Asked by At

Missing some connection which I am unable to figure out (also googled a lot still no success) in how to open PDF file in app. I am using PDFKit. This is the PDFKitView struct in which URL should be passed:

    struct PDFKitView: View {
    @State var reportId: ReportResponse
    var url:URL
    var body:some View
        {
        PDFKitRepresentedView(url)
        }       
     }

        struct PDFKitRepresentedView: UIViewRepresentable{
            func updateUIView(_ uiView: UIView, context: 
                  UIViewRepresentableContext<PDFKitRepresentedView>) {
                //
             }

       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
        }  
      }

This is the Report Row in which I am trying to pass URL to state object pdfDonwload:

struct ReportRow: View {
var report : ReportResponse
@StateObject var pdfDownload:URL = URL(fileURLWithPath: "")

var body: some View {
        VStack{
            HStack{
                Text(report.name)
                    //formatting
                }.frame(maxWidth: .infinity, alignment: .leading)
            
            HStack{
                    Text("P.Id:")
                         //formatting
                    Text(report.patientID)
                         //formatting
            
        Spacer()
                    Text("Status")
                         //formatting
                    Text(report.status)
                         //formatting
            }}
             .onAppear{
                  Task{ 
                     do{
                         try await getPath()
                        }catch{Alert(title:"Text")}
                       }
                     }}
          func getPath() async throws
            {
             var urlComponents = URLComponents()
          //add other components
         urlComponents.queryItems = [URLQueryItem(name: 
          "uniquePackageId", value: 
          uniqueReportId)]
          let url = urlComponents.url
    let downloadTask = URLSession.shared.downloadTask(with: url!)
    {
        urlOrNil, responseOrNil, errorOrNil in
        guard let fileURL = urlOrNil else {return}
                do
                {
                let documentURL = try FileManager.default.url(for: 
                  .documentDirectory, in: 
                .userDomainMask, appropriateFor: nil, create: false)
                let savedURL = documentURL.appendingPathComponent("\ 
                (self.patientName)_\(UUID().uuidString).pdf")

                print(savedURL)

                try FileManager.default.moveItem(at: fileURL, to: 
                savedURL)
                          
                   DispatchQueue.main.async {
                                pdfDownload = url!
                          }
                      }
                    catch
                      {
                          print("Error")
                      }}                 
               downloadTask.resume()
     }}

However, no value is passed to PDFKitView() it is blank. The first issue is PDFKitView should get the updated pdfDownload:URL value from the func call. Neither the value is updating nor passing updated value to PDFKitView.

This is the list struct:

       struct ReportList: View{
           @ObserveObjecct var reportLink:ReportViewModel
           @State var pdfDownload:URL = URL(fileURLWithPath="")
           
       var body:some view{
     NavigationView{
       List{
        ForEach(reportLink.trackReport)
            {report in 
        VStack{NavigationLink
        (destination:PDFKitView(url:pdfDownload, 
                      pdfDownload:pdfDownload,report:report))
                {
                  ReportRow(report:report)
                }
             }}}}}}
             

I want file should open automatically after downloading. Running the app in simulator.

0

There are 0 answers