0

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.

tintin
  • 335
  • 2
  • 8
  • do not put `PDFKitView` or any View inside a `Button` action. Read the SwiftUI basics again, here https://developer.apple.com/tutorials/swiftui/ This is the source of your error and your problem. – workingdog support Ukraine Apr 30 '22 at 13:20
  • @workingdogsupportUkraine hi made some changes pls look....i searched a lot googled....but this same problem has no answer where file should open automatically after downloading....pls guide stuck in this issue for days.....its very important – tintin May 01 '22 at 06:58
  • There are too many missing parts, too many spelling mistakes etc...to understand what is going on and what you are trying to achieve. I suggest you isolate your problem and show a simple minimal and reproducible example code. – workingdog support Ukraine May 01 '22 at 07:57
  • @workingdogsupportUkraine trying to very simple thing those ans is hard to find....there is a list..on clicking any row of list the corresponding pdf should be displayed...a unique id sent to API which in response will provide the pdf file.....i successfully downloaded the pdf in docudirectory but unable to open it after downloading that is file should open directly after downloading (or directly by providing API url)...not a single solution on net – tintin May 01 '22 at 12:15
  • this is the ans as provided by @workingdogsupportUkraine thank you very much.....it was a simple thing which I missed...thank you again was going mad over this issue https://stackoverflow.com/a/72096369/14619182 – tintin May 04 '22 at 10:57

0 Answers0