Post

Replies

Boosts

Views

Activity

Reply to swift mac swift app fails to show image view crashes on first run
Yes, using UserDefaults directly and after the saving of persistent url. Yes have @main. Here is the PictureWindowApp.swift: import SwiftUI @main struct PictureWindowApp: App {     @ObservedObject var appState = AppState()          var body: some Scene {         WindowGroup {             ContentView(appState: appState)                 .edgesIgnoringSafeArea(.all)                 .onReceive(NotificationCenter.default.publisher(for: NSApplication.didBecomeActiveNotification), perform: { _ in                     NSApp.mainWindow?.standardWindowButton(.zoomButton)?.isHidden = true                     NSApp.mainWindow?.standardWindowButton(.closeButton)?.isHidden = true                     NSApp.mainWindow?.standardWindowButton(.miniaturizeButton)?.isHidden = true                 })         }         .windowStyle(.hiddenTitleBar)         .windowToolbarStyle(UnifiedCompactWindowToolbarStyle())         .commands {             PictureWindowCommands(appState: appState)         }     } }
Topic: UI Frameworks SubTopic: AppKit Tags:
May ’22
Reply to swift mac swift app fails to show image view crashes on first run
Have narrowed down problem. When first run, there is no image, and have user open a local file. I then create and save the bookmark for that, so on subsequent run, can access files based on user having already done so. If I call the function to save bookmark, it saves successfully, but when gets to image view, fails to display the image but program continues to run as if all is well. And on next run it retrieves bookmark and opens file and all works fine. IF I DON'T save bookmark, the program goes on to work and displays image all is well. But on next run of course, fails since there is no bookmark saved. Here is function that saves bookmark: func saveBookmarkForSelectedURL(fileURL: URL?) -> Bool {         print("enter saveBookmarkForSelectedURL (String(describing: fileURL))")         do {             if let selectedURL =  fileURL {                 // Create a security-scoped bookmark                 // "Returns bookmark data for the URL, created with specified options and resource values."                 // Return Value: Type: Data - A bookmark for the URL.                 let bookmarkData = try selectedURL.bookmarkData(options: .withSecurityScope, includingResourceValuesForKeys: nil, relativeTo: nil)                 // get an instance of UserDefaults                 let userDefaults = UserDefaults.standard                 //  store the newly-created bookmark in UserDefaults                 // accessible with a meaningful key; user defaults supports storing                 // and fetching the Data/NSData types                 userDefaults.set(bookmarkData, forKey: "PermanentFileBookmark")                 // creating the bookmark did not throw an error, so return positive                 print("just stored bookmark (bookmarkData)")                 return true             } else {                 print("ERROR: You cannot bookmark a URL that is nil")                 return false             }         } catch let error {             print("Could not create a bookmark because: ", error)             return false  }     } // end func saveBookmarkForSelectedURL()
Topic: UI Frameworks SubTopic: AppKit Tags:
Jun ’22