Post

Replies

Boosts

Views

Activity

Reply to how to change appInfo in SwiftUI?
@Environment(\.openWindow) var openWindow Window("About", id: "about") {     MyAboutView() } .commands {     CommandGroup(replacing: .appInfo) {         Button(action: {             openWindow(id: "about")         }, label: {             Text("About MyAPP")         })     } }
Feb ’23
Reply to Custom File "New Document" to show template-picker in a document-based app
Answering my own question. I want to use Commands. See newItem. Here's an example: struct DocDemoApp: App {     @Environment(\.openWindow) var openWindow     @Environment(\.openDocument) private var openDocument     var body: some Scene {         DocumentGroup(newDocument: { DocDemoDocument() }) { config in             ContentView(document: config.document)         }         Window("Choose Template", id: "wizard") {             NewDocWizard()         }         .commands {             CommandGroup(replacing: .newItem) {                 Section {                     // Menu File >                     Button("New") { openWindow(id: "wizard") }.keyboardShortcut(KeyboardShortcut("N"))                     Button("Open") { open() }.keyboardShortcut(KeyboardShortcut("o"))                     Button("Open Recent...") { print("NYI") }                 }             }             CommandGroup(replacing: .singleWindowList) {                 // Menu Window > Choose Template, ... (list of single windows)                 // Hide and force user to use File > New for this window             }         }     }     private func open() {         let panel = NSOpenPanel()         panel.allowedContentTypes = [.exampleText]         panel.allowsMultipleSelection = true         panel.canChooseDirectories = false         panel.runModal()         if let url = panel.url {             Task {                 try! await openDocument(at: url)             }         }     } }
Jan ’23
Reply to (Xcode 14.0 beta 5) ThreadSanitizer: CHECK failed: tsan_platform_posix.cpp:83 "((beg)) <= ((end))"
On my Intel MBP is get a different error when the thread sanitizer is on. "malloc: nano zone abandoned due to inability to preallocate reserved vm space." StackOverflow suggests this is relatively harmless, but paired with the error I get on my Mac Mini (Apple Silicon) There's something afoul. [https://stackoverflow.com/questions/64126942/malloc-nano-zone-abandoned-due-to-inability-to-preallocate-reserved-vm-space]
Aug ’22
Reply to Attempt to map database failed
I put together a tiny demo using UIActivityViewController sharing a String. This also gets the "log noise" as Quinn so politely puts it. (My own choice term is not suitable for this forum.) I was surprised since I hadn't seen these message a day before, but maybe it correlates with beta 7.
Aug ’21
Reply to printing with swiftui
I've seen Paul Hudson's sample on simple printing, (https://www.hackingwithswift.com/example-code/uikit/how-to-print-using-uiactivityviewcontroller). I'm still looking for how to present a system share sheet, which should allow me to "export" data to whatever form a share action wants, be it printing, tweeting, emailing, and so on. (Apple forums doesn't like me to post live URLs to that site: This domain "https://www.hackingwithswift.com/example-code/uikit/how-to-print-using-uiactivityviewcontroller" is not a permitted domain on this forums.) No sooner do I post this than I finally find an example that looks like it'll work (https://jeevatamil.medium.com/how-to-create-share-sheet-uiactivityviewcontroller-in-swiftui-cef64b26f073). I'll try to remember to post working code that connects this to printing.
Aug ’21
Reply to How to implement custom type for onDrag and onInsert
Answering my own question. .onInsert only works with List. Use onDrop instead. The custom type should be declared final class MyType: NSObject, NSItemProviderWriting, NSItemProviderReading, Codable Here are some relevant code snippets that I hope help the next wanderer... import UniformTypeIdentifiers let MyTypeUTI: String = UTType.data.identifier // Very suspicious - will this allow ANY public.data to be dropped? Bad. final class MyType: NSObject, NSItemProviderWriting, NSItemProviderReading, Codable {     static var readableTypeIdentifiersForItemProvider: [String] = [MyTypeUTI]     static func object(withItemProviderData data: Data, typeIdentifier: String) throws -> Self {         let jsonDecoder = JSONDecoder()         let item = try! jsonDecoder.decode(Self.self, from: data)         return item     }     public enum Oops: Error {         case invalidTypeIdentifier     }     static var writableTypeIdentifiersForItemProvider: [String] = [MyTypeUTI]     func loadData(withTypeIdentifier typeIdentifier: String, forItemProviderCompletionHandler completionHandler: @escaping (Data?, Error?) -> Void) -> Progress? {         let progress = Progress(totalUnitCount: 100)         guard typeIdentifier == MyTypeUTI else { completionHandler(nil, Oops.invalidTypeIdentifier)             return progress         }         do {             let jsonEncoder = JSONEncoder()             let data = try jsonEncoder.encode(self)             completionHandler(data, nil)         }         catch { completionHandler(nil, error) }         return progress     } ...[The rest of MyType here]... Then to implement drag and drop in your View: ScrollView { VStack { ForEach(...) { item in ItemView(item)                     .onDrag({ NSItemProvider(object: item) })                     .onDrop(of: [MyTypeUTI],                             delegate: MyTypeDropDelegate(item: item, ...) Your DropDelegate might be like this (very rough code): struct MyTypeDropDelegate: DropDelegate {     let item: MyType     func performDrop(info: DropInfo) -> Bool {         let providers = info.itemProviders(for: [FigureBoxUTI])         guard let provider = providers.first else {             return false         }         provider.loadObject(ofClass: FigureBox.self) { item, error in             guard let box = box as? FigureBox else { ...handle error...                 return             }             ...handle drop of item...         }         return true     } } To be improved: I think that MyTypeUTI needs to be a custom string which extends public.data. UTIs are still a bit of a mystery to me since trying to use UTType was causing errors for me.
Aug ’21