Post

Replies

Boosts

Views

Activity

EnvironmentModel in Document app - per Document?
I have a SwiftUI Document app that successfully creates multiple documents using a structure I took from a WWDC example. In this, the environment model is common to every app, so if I change something (e.g. a published variable setting) in one document it affects all open documents. I have been trying to find an example where at least some of the published variables currently in the app level environment can be split out to be made document specific. i.e. only applicable to windows on that document, but still without having to pass them round to every view. Any pointers very welcome - haven't been able to find anything so far...
0
0
178
Aug ’23
Different iPad/iPhone versions of the same app?
I have an app that I want to publish for Mac, iPhone and iPad. The iPad version I have written uses a similar SplitNavigationView as the Mac, but this doesn't work on iPhone so I have an iPhone version that uses sheets. Unfortunately, as I understand it, you can't have two iOS builds in one app in App Store Connect. I just wanted to check my understanding of feedback on this from Apple Developer support. I take from the discussion with them that I have a choice of 1) not publishing an iPhone version, 2) re-writing an iOS version that runs on both iPhone and iPad, or 3) publishing the iPhone or iPad version under a different name. (I see this is the approach used by the to-do app, Things 3, for instance.) Is this correct?
1
0
411
May ’23
ImageRenderer in SwiftUI with EnvironmentObject?
I have an app using a view model EnvironmentObject. I want to add a Print to PDF ShareLink but the rendered image doesn't have access to the environment object. The code below replicates the problem in shorter form, the run time error being: Fatal error: No ObservableObject of type Model found. A View.environmentObject(_:) for Model may be missing as an ancestor of this view. Is there anyway to get the environmentObject into the rendered view? or an alternative way to implement Print to PDF with a viewModel based app? import SwiftUI @main struct ImageRendererWithEnvironmentApp: App { var body: some Scene { WindowGroup { ContentView() } } } // // ContentView.swift // ImageRendererWithEnvironment // // Created by Stuart on 18/03/2023. // import SwiftUI struct ContentView: View { @EnvironmentObject var model: Model var myView: some View { return createMyView(text: "Help me!") } var body: some View { VStack { myView ShareLink("Print to PDF", item: render()) } } private func createMyView(text: String) -> some View { VStack { ZStack { RoundedRectangle(cornerRadius: 20) .fill(model.backgroundColor) .frame(width: 300, height: 200) VStack { Image(systemName: "globe") .imageScale(.large) Text(text) } .foregroundColor(.white) .padding() } } } func render() -> URL { let renderer = ImageRenderer(content: myView) let url = URL.documentsDirectory.appending(path: "output.pdf") renderer.render { size, context in var box = CGRect(x: 0, y:0, width: size.width, height: size.height) guard let pdf = CGContext(url as CFURL, mediaBox: &box, nil) else { return } pdf.beginPDFPage(nil) context(pdf) pdf.endPDFPage() pdf.closePDF() } return url } } class Model: ObservableObject { // Publish the decision being worked on (will be worked as EnvironmentObject and occasionally saved to CoreData) @Published var backgroundColor: Color = .red }
1
0
656
Mar ’23