I am working on an app that will allow users to share or save the view via a share sheet. To capture the view I am using the ImageRenderer. When I go to click on the share button, it does not work and says, "Unable to render flattened version of PlatformViewControllerRepresentableAdaptor<MulticolumnSplitViewRepresentable<ModifiedContent<Element, NavigationColumnModifier>, Never, _UnaryViewAdaptor>>." When I go to the issues it will say, "Context in environment is not connected to a persistent store coordinator: <NSManagedObjectContext: 0x6000014851e0>." How can I fix this.
This is the code.
struct DividendView: View {
@Environment(\.managedObjectContext) var moc
@FetchRequest(sortDescriptors: [
SortDescriptor(\.stock_name)
]) var dividends: FetchedResults<Dividend>
@State private var showingAddScreen = false
@State private var showingShare = false
// @EnvironmentObject var vm = ViewModel()
@State private var renderedImage: Image?
@State private var selectedName: String?
@State private var expanded: Bool = false
let dividend: Dividend
// @State private var items: [Any] = []
// @State private var sheet: Bool = false
// let columns = [GridItem(.fixed(50)), GridItem(.fixed(50)), GridItem(.fixed(50)), GridItem(.fixed(50)), GridItem(.fixed(50))]
var body: some View {
NavigationView {
ScrollView(.horizontal) {
VStack {
HStack {
Text("Stock Name")
.frame(width: 70)
.padding(.leading, 15)
.padding()
.font(.system(size: 10, weight: .heavy, design: .rounded))
.font(.largeTitle)
// .background(Color.blue)
Text("Num of Shares")
.padding()
.frame(width: 100)
.font(.system(size: 10, weight: .heavy, design: .rounded))
.font(.largeTitle)
// .background(Color.blue)
Text("Dividend per Share")
.padding()
.frame(width: 130)
.font(.system(size: 10, weight: .heavy, design: .rounded))
.font(.largeTitle)
// .background(Color.blue)
Text("Franking")
.padding()
.frame(width: 100)
.font(.system(size: 10, weight: .heavy, design: .rounded))
.font(.largeTitle)
// .background(Color.blue)
Text("Dividend Date")
.frame(width: 80)
.padding()
.font(.system(size: 10, weight: .heavy, design: .rounded))
.font(.largeTitle)
// .background(Color.blue)
Text("Payment Date")
.frame(width: 80)
.padding()
.font(.system(size: 10, weight: .heavy, design: .rounded))
.font(.largeTitle)
//.background(Color.blue)
Text(" ")
// }
}
// LazyVGrid(columns: columns) {
List {
ForEach(dividends) { dividend in
HStack {
Text(dividend.stock_name ?? "No Name")
// .background(Color.red)
.padding()
.frame(width: 100)
.font(.system(size: 10, weight: .medium, design: .serif))
.font(.title2)
Text(dividend.num_shares ?? "No shares")
// .background(Color.red)
.padding()
.frame(width: 100)
.font(.system(size: 10, weight: .medium, design: .serif))
.font(.title2)
let dps = dividend.dividend_per_share
let theDPS = Double(dps ?? "0") ?? 0
let actualDPS = Double(theDPS)
Text("$\(actualDPS, specifier: "%.2f")")
// .background(Color.red)
.padding()
.frame(width: 115)
.font(.system(size: 10, weight: .medium, design: .serif))
.font(.title2)
Text(dividend.franking ?? "No franking")
// .background(Color.red)
.padding()
.frame(width: 115)
.font(.system(size: 10, weight: .medium, design: .serif))
.font(.title2)
Text(dividend.dividend_date ?? "No date")
// .background(Color.red)
.padding()
.frame(width: 115)
.font(.system(size: 10, weight: .medium, design: .serif))
.font(.title2)
Text(dividend.payment_date ?? "No date")
// .background(Color.red)
.padding()
.frame(width: 100)
.font(.system(size: 10, weight: .medium, design: .serif))
.font(.title2)
Text(" ")
}
}
.onDelete(perform: deleteBooks)
}
.listStyle(.plain)
}
.navigationTitle("Dividend Income")
.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
EditButton()
}
ToolbarItem(placement: .navigationBarTrailing) {
Button {
showingAddScreen.toggle()
} label: {
Label("Add Book", systemImage: "plus")
}
}
ToolbarItem(placement: .navigationBarLeading) {
Button {
if #available(iOS 16.0, *) {
let renderer = ImageRenderer(content: DividendView(dividend: Dividend()))
// renderer.scale = 3
if let image = renderer.cgImage {
renderedImage = Image(decorative: image, scale: 1.0)
}
}
// .buttonStyle(.borderedProminent)
// RenderedImageView(renderedImage: $renderedImage)
// .navigationTitle("Business Card")
} label: {
Label("Share", systemImage: "square.and.arrow.up")
}
}
}
.sheet(isPresented: $showingAddScreen) {
AddDividendView()
}
.sheet(isPresented: $showingShare) {
if let renderedImage {
if #available(iOS 16.0, *) {
ShareLink(item: renderedImage, preview: SharePreview("My card", image: renderedImage))
}
}
}
}
}
}
func deleteBooks(at offsets: IndexSet) {
for offset in offsets {
let dividend = dividends[offset]
moc.delete(dividend)
}
try?moc.save()
}
}
struct DividendView_Preview: PreviewProvider {
static var previews: some View {
DividendView(dividend: Dividend())
}
}