Post

Replies

Boosts

Views

Activity

Reply to WidgetKit with Data from CoreData
Thanks for the tip, darkpaw. But I'm afraid you misunderstand my question. It's underlined. Do I have to let the widget pull data directly from CoreData data manager (DataManager)? In other words, is there any way I could use my existing view model (ViewModel) to deliver data to the widget? You seem to be writing your fetchRecords() method to get your data inside the getTimeline() method, i.e.: That should be put into your DataManager() class. Okay. Thanks. I have done all dirty work through my view model (ViewModel) including the data sorting. That's why I want to know if I need to do it with the data manager, again, just for the widget. That's what I mean by 'redundant' or superfluous.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
May ’25
Reply to WidgetKit with Data from CoreData
A couple of articles that I have read suggest that I get CoreData records from my DataManager like the following. It's kind of redundant as it accesses DataManager inside the TimelineProvider struct and also inside the Widget struct. That's why I wonder if this is the right approach. import WidgetKit import SwiftUI import CoreData struct Provider: TimelineProvider { func placeholder(in context: Context) -> SimpleEntry { ... } func getSnapshot(in context: Context, completion: @escaping (SimpleEntry) -> ()) { ... } func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> ()) { let manager = CloudDataManager() var records: [Little] = [] let request = NSFetchRequest<Little>(entityName: "Little") do { records = try manager.context.fetch(request) records.sort { lhs, rhs in lhs.trashDate! < rhs.trashDate! } } catch { print("Fetch error for CloudDataManager: \(error.localizedDescription)") } var items: [Item] = [] for record in records { let item = Item(trashDate: record.trashDate ?? Date.now, imageSelection: Int(record.imageSelection)) items.append(item) } let entry = Timeline(entries: [SimpleEntry(date: Date(), items: items)], policy: .atEnd) completion(entry) } } struct SimpleEntry: TimelineEntry { let date: Date let items: [Item] } struct LittleGuyEntryView : View { var entry: Provider.Entry var body: some View { ZStack { ContainerRelativeShape() .fill(.widgetBackground) VStack { if entry.items.count > 0 { ForEach(entry.items, id: \.id) { item in ... } } } .padding() } } } struct LittleGuy: Widget { let manager = DataManager() let kind: String = "App name" var body: some WidgetConfiguration { StaticConfiguration(kind: kind, provider: Provider()) { entry in LittleTrashEntryView(entry: entry) .environment(\.managedObjectContext, manager.context) .containerBackground(.fill.tertiary, for: .widget) } .contentMarginsDisabled() .supportedFamilies([.systemSmall]) } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
May ’25
Reply to onReceive(_:perform:) on Frontmost Window Only?
In reference to this article ( https://nilcoalescing.com/blog/ProvidingTheCurrentDocumentToMenuCommands/ ), you can do it with @FocusedBinding. I don't even know what this @FocusedBinding guy is. It's been around since macOS 11? Ugh... import SwiftUI @main struct FocusedBindingCrazyMamaApp: App { var body: some Scene { DocumentGroup(newDocument: FocusedBindingCrazyMamaDocument()) { file in ContentView(document: file.$document) .focusedSceneValue(\.focusedDocument, file.$document) } .commands { CommandGroup(replacing: .saveItem) { UppercasedText() LowercasedText() } } } } extension FocusedValues { struct DocumentFocusedValues: FocusedValueKey { typealias Value = Binding<FocusedBindingCrazyMamaDocument> } var focusedDocument: Binding<FocusedBindingCrazyMamaDocument>? { get { self[DocumentFocusedValues.self] } set { self[DocumentFocusedValues.self] = newValue } } } struct UppercasedText: View { @FocusedBinding(\.focusedDocument) var focusedDocument var body: some View { Button { guard let text = focusedDocument?.text else { return } focusedDocument?.text = text.uppercased() } label: { Text("Uppercase Text") } .disabled(focusedDocument == nil) .keyboardShortcut("u", modifiers: [.command, .shift]) } } struct LowercasedText: View { @FocusedBinding(\.focusedDocument) var focusedDocument var body: some View { Button { guard let text = focusedDocument?.text else { return } focusedDocument?.text = text.lowercased() } label: { Text("Lowercase Text") } .disabled( focusedDocument == nil ) .keyboardShortcut("l", modifiers: [.command, .shift]) } } // ContentView.swift // import SwiftUI struct ContentView: View { @Binding var document: FocusedBindingCrazyMamaDocument var body: some View { TextEditor(text: $document.text) .foregroundStyle(.white) .font(.system(size: 24)) .scrollContentBackground(.hidden) .background(.black) } } #Preview { ContentView(document: .constant(FocusedBindingCrazyMamaDocument())) }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
May ’25
Reply to Opening a New Document from File URL?
Taking a look at openDocument, it says it can open document at a specific URL, provided that its bookmark is resolved. And it does open a file with security-scoped bookmark. func loadBookmarks() async { for bookmarkItem in bookmarkViewModel.bookmarkItems { // resolving a bookmark if let _ = resolveBookmark(bookmarkData: bookmarkItem.bookmarkData) { do { try await openDocument(at: bookmarkItem.bookmarkURL) } catch { print("\(error.localizedDescription)") } } } } struct BookmarkItem: Codable, Hashable { let bookmarkURL: URL let date: Date let bookmarkData: Data let open: Bool }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
May ’25
Reply to First App Submission — Apple can’t see my In-App Purchase product during review (works in debug, not in TestFlight)
Here’s what’s going on: • In debug mode via Xcode, the product shows up correctly, and everything works as expected. • In TestFlight builds, the product doesn’t show up at all — neither for me nor for Apple. • The IAP is currently marked as “Waiting for Review”, not “Ready to Submit”, and it is linked to the current app version in App Store Connect. Pardon me, but what does the TestFlight version of your app have to do with an issue you are currently having? If they say they can't see an in-app purchase product, ask them for a screenshot or two, which they usually do. There is a section where you need to submit an IAP product for review. Have you done that? I happen to have submitted an application to MAS with an IAP today. And I've done that.
Apr ’25
Reply to Clearing Change Count in FileDocument?
I see. I didn't think about it. Thank you, DTS Engineer. I'll try it later. I tried using undoManager with TextEdit under macOS two days ago. The macOS application consistently crashed when I tried to 'redo' it. If I remember correctly, the exact same lines of code didn't crash under iOS.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Apr ’25
Reply to Problems Publishing with User Tracking
I've indicated that I don't use this data for ads, that it's only used for personalization and to understand who saves items. What does that mean? It doesn't sound like a grammatically-correct English sentence. I added the NSUserTrackingUsageDescription property to the info.plist. What does it say?
Topic: Community SubTopic: Apple Developers Tags:
Mar ’25
Reply to WidgetKit with Data from CoreData
Thanks for the tip, darkpaw. But I'm afraid you misunderstand my question. It's underlined. Do I have to let the widget pull data directly from CoreData data manager (DataManager)? In other words, is there any way I could use my existing view model (ViewModel) to deliver data to the widget? You seem to be writing your fetchRecords() method to get your data inside the getTimeline() method, i.e.: That should be put into your DataManager() class. Okay. Thanks. I have done all dirty work through my view model (ViewModel) including the data sorting. That's why I want to know if I need to do it with the data manager, again, just for the widget. That's what I mean by 'redundant' or superfluous.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
May ’25
Reply to WidgetKit with Data from CoreData
A couple of articles that I have read suggest that I get CoreData records from my DataManager like the following. It's kind of redundant as it accesses DataManager inside the TimelineProvider struct and also inside the Widget struct. That's why I wonder if this is the right approach. import WidgetKit import SwiftUI import CoreData struct Provider: TimelineProvider { func placeholder(in context: Context) -> SimpleEntry { ... } func getSnapshot(in context: Context, completion: @escaping (SimpleEntry) -> ()) { ... } func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> ()) { let manager = CloudDataManager() var records: [Little] = [] let request = NSFetchRequest<Little>(entityName: "Little") do { records = try manager.context.fetch(request) records.sort { lhs, rhs in lhs.trashDate! < rhs.trashDate! } } catch { print("Fetch error for CloudDataManager: \(error.localizedDescription)") } var items: [Item] = [] for record in records { let item = Item(trashDate: record.trashDate ?? Date.now, imageSelection: Int(record.imageSelection)) items.append(item) } let entry = Timeline(entries: [SimpleEntry(date: Date(), items: items)], policy: .atEnd) completion(entry) } } struct SimpleEntry: TimelineEntry { let date: Date let items: [Item] } struct LittleGuyEntryView : View { var entry: Provider.Entry var body: some View { ZStack { ContainerRelativeShape() .fill(.widgetBackground) VStack { if entry.items.count > 0 { ForEach(entry.items, id: \.id) { item in ... } } } .padding() } } } struct LittleGuy: Widget { let manager = DataManager() let kind: String = "App name" var body: some WidgetConfiguration { StaticConfiguration(kind: kind, provider: Provider()) { entry in LittleTrashEntryView(entry: entry) .environment(\.managedObjectContext, manager.context) .containerBackground(.fill.tertiary, for: .widget) } .contentMarginsDisabled() .supportedFamilies([.systemSmall]) } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
May ’25
Reply to onReceive(_:perform:) on Frontmost Window Only?
In your case, can't you add a user info, with the ID of the window for instance ? And test in each window if the notification if for itself. Thanks for the tip. I don't know if that works. You wouldn't still be able to tell which tab is the one currently being selected, I suppose.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
May ’25
Reply to Opening FileDocument with URL → should only be called in the main thread
Try using the .task view modifier and removing the Task {} from the function instead. Thanks. But that's not really the main issue here.
Topic: UI Frameworks SubTopic: General Tags:
Replies
Boosts
Views
Activity
May ’25
Reply to Opening FileDocument with URL → should only be called in the main thread
Okay. I get it. If I look at openDocument, it's labeled @Environment @MainActor That's where the main thread issue comes to surface, I guess.
Topic: UI Frameworks SubTopic: General Tags:
Replies
Boosts
Views
Activity
May ’25
Reply to onReceive(_:perform:) on Frontmost Window Only?
In reference to this article ( https://nilcoalescing.com/blog/ProvidingTheCurrentDocumentToMenuCommands/ ), you can do it with @FocusedBinding. I don't even know what this @FocusedBinding guy is. It's been around since macOS 11? Ugh... import SwiftUI @main struct FocusedBindingCrazyMamaApp: App { var body: some Scene { DocumentGroup(newDocument: FocusedBindingCrazyMamaDocument()) { file in ContentView(document: file.$document) .focusedSceneValue(\.focusedDocument, file.$document) } .commands { CommandGroup(replacing: .saveItem) { UppercasedText() LowercasedText() } } } } extension FocusedValues { struct DocumentFocusedValues: FocusedValueKey { typealias Value = Binding<FocusedBindingCrazyMamaDocument> } var focusedDocument: Binding<FocusedBindingCrazyMamaDocument>? { get { self[DocumentFocusedValues.self] } set { self[DocumentFocusedValues.self] = newValue } } } struct UppercasedText: View { @FocusedBinding(\.focusedDocument) var focusedDocument var body: some View { Button { guard let text = focusedDocument?.text else { return } focusedDocument?.text = text.uppercased() } label: { Text("Uppercase Text") } .disabled(focusedDocument == nil) .keyboardShortcut("u", modifiers: [.command, .shift]) } } struct LowercasedText: View { @FocusedBinding(\.focusedDocument) var focusedDocument var body: some View { Button { guard let text = focusedDocument?.text else { return } focusedDocument?.text = text.lowercased() } label: { Text("Lowercase Text") } .disabled( focusedDocument == nil ) .keyboardShortcut("l", modifiers: [.command, .shift]) } } // ContentView.swift // import SwiftUI struct ContentView: View { @Binding var document: FocusedBindingCrazyMamaDocument var body: some View { TextEditor(text: $document.text) .foregroundStyle(.white) .font(.system(size: 24)) .scrollContentBackground(.hidden) .background(.black) } } #Preview { ContentView(document: .constant(FocusedBindingCrazyMamaDocument())) }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
May ’25
Reply to Opening a New Document from File URL?
Taking a look at openDocument, it says it can open document at a specific URL, provided that its bookmark is resolved. And it does open a file with security-scoped bookmark. func loadBookmarks() async { for bookmarkItem in bookmarkViewModel.bookmarkItems { // resolving a bookmark if let _ = resolveBookmark(bookmarkData: bookmarkItem.bookmarkData) { do { try await openDocument(at: bookmarkItem.bookmarkURL) } catch { print("\(error.localizedDescription)") } } } } struct BookmarkItem: Codable, Hashable { let bookmarkURL: URL let date: Date let bookmarkData: Data let open: Bool }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
May ’25
Reply to First App Submission — Apple can’t see my In-App Purchase product during review (works in debug, not in TestFlight)
Here’s what’s going on: • In debug mode via Xcode, the product shows up correctly, and everything works as expected. • In TestFlight builds, the product doesn’t show up at all — neither for me nor for Apple. • The IAP is currently marked as “Waiting for Review”, not “Ready to Submit”, and it is linked to the current app version in App Store Connect. Pardon me, but what does the TestFlight version of your app have to do with an issue you are currently having? If they say they can't see an in-app purchase product, ask them for a screenshot or two, which they usually do. There is a section where you need to submit an IAP product for review. Have you done that? I happen to have submitted an application to MAS with an IAP today. And I've done that.
Replies
Boosts
Views
Activity
Apr ’25
Reply to Can I submit my application, which have not public usage?
It sounds to me that you are trying to publish something that is just a copy of your website, which may go against App Review Guideline 4.2. So what could your app do that your website can't?
Replies
Boosts
Views
Activity
Apr ’25
Reply to Different Build Schemes -> Error: -Onone Swift optimization level to use previews
I have seen somebody else's project where the Preview guy works under the Release build mode. I now presume that this Release mode is just a code of the original Debug mode.
Replies
Boosts
Views
Activity
Apr ’25
Reply to Opening a New Tab with Text in a Document-Based App
Thank you, DTS Engineer. It works. I forgot that I had had the newDocument environment thing 4 days earlier. I'm too old for new things. I didn't know about the tab-opening preference under Desktop & Dock.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Apr ’25
Reply to Clearing Change Count in FileDocument?
I see. I didn't think about it. Thank you, DTS Engineer. I'll try it later. I tried using undoManager with TextEdit under macOS two days ago. The macOS application consistently crashed when I tried to 'redo' it. If I remember correctly, the exact same lines of code didn't crash under iOS.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Apr ’25
Reply to Not Showing FileOpen with Document-
Thanks a lot, DTS Engineer. I guess defaultLaunchBehavior(_:) is available for macOS 15.0 or higher. I guess I have to switch back to Cocoa with Storyboard since I also need to use TextSelection.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Apr ’25
Reply to Problems Publishing with User Tracking
I've indicated that I don't use this data for ads, that it's only used for personalization and to understand who saves items. What does that mean? It doesn't sound like a grammatically-correct English sentence. I added the NSUserTrackingUsageDescription property to the info.plist. What does it say?
Topic: Community SubTopic: Apple Developers Tags:
Replies
Boosts
Views
Activity
Mar ’25
Reply to This bundle is invalid - Your archive contains paths that are not allowed: [._Symbols]
Use the Terminal to list all files with the 'ls -a' command?
Replies
Boosts
Views
Activity
Mar ’25