Issue
I'm try to share a PDF document which is display using Quick Look view.
I can correctly see the PDF loaded in the PDF view but I can't add the PDF path to the arrayItemToShare.
I have tried many things but can't find the reason why this code not adding anything!
arrayItemToShare?.append(report.pathFile!)
below the code of my report view.
struct listReport: View {
@ObservedObject var dm : DataManager
var report : UserReport
@State var openQL = false
@State var share = false
@State var arrayItemToShare : [String]?
var body: some View {
Button(action: {
openQL.toggle()
}, label: {
VStack(alignment: .center, spacing: 60){
if report.urlFile != nil {
PDFThumbnailRepresented(urlDoc: report.urlFile!).padding()
}
Spacer()
Text(report.id_report_show).foregroundColor(.primary).bold()
}
})
.sheet(isPresented: $openQL, content: { // open sheet to preview PDF
fakebar
if report.pathFile != nil {
QLView(filePath: report.pathFile!, isPresented: $openQL) // open Quick Look and preview the file pdf at path
.onAppear {
arrayItemToShare?.append(report.pathFile!) // why this not adding the value to the array
print("count \(arrayItemToShare!.count)") // here the crash because array is NIL
}
} else {
Text("OPS!!.. unable to load pdf")
}
})
.sheet(isPresented: $share) {
ShareSheet(item: arrayItemToShare!)
}
}
var fakebar: some View {
ZStack {
HStack {
Button(action: {
if report.urlFile != nil {
openQL.toggle() // close preview pdf
share.toggle() // open share sheet
}
}) {
Image(systemName: "square.and.arrow.up")
.foregroundColor(.white)
.padding(.horizontal)
}
Spacer()
Image(systemName: "chevron.compact.down")
.font(.system(size: 60))
.aspectRatio(contentMode: .fit)
.foregroundColor(.white)
Spacer()
Button(action: {
openQL.toggle()
}) {
Text("Close")
.fontWeight(.bold)
.foregroundColor(.white)
.padding(.horizontal)
}
}
}
.frame(height: 44)
.background(Color("AMUColor").padding(.top, -44))
}
}
I use this (listReport: View) inside a scrollView with a foreach loop, where everything work fine.
Solution
Here is a fix:
QLView(filePath: report.pathFile!, isPresented: $openQL) // open Quick Look and preview the file pdf at path
.onAppear {
if nil == arrayItemToShare {
arrayItemToShare = [String]() // << create if needed !!
}
arrayItemToShare?.append(report.pathFile!)
print("count \(arrayItemToShare!.count)")
}
Answered By - Asperi Answer Checked By - Terry (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.