Solved. I had stupid dead code.
Post
Replies
Boosts
Views
Activity
Would safeAreaInset, applied to the button you want to be tappable, help? (I have not dealt a lot with safe areas so I am guessing.)
How to inset the safe area with custom content:
https://www.hackingwithswift.com/quick-start/swiftui/how-to-inset-the-safe-area-with-custom-content
Just don't use .automatic.
My trouble was stale cache in the simulator or Mac. Restarting/rebooting a couple times helped.
(Don't post while sleepy)
Right in the docs... .modelContainer(for: [MyModel1.self, MyModel2.self], isUndoEnabled: true)
@Environment(\.openWindow) var openWindow
Window("About", id: "about") {
MyAboutView()
}
.commands {
CommandGroup(replacing: .appInfo) {
Button(action: {
openWindow(id: "about")
}, label: {
Text("About MyAPP")
})
}
}
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)
}
}
}
}
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]
I created a blank iOS app project in Xcode, enabled the thread sanitizer and it crashes exactly the same way on two simulators. I'm going to try a few more things to see how much might be me or Xcode.
I found that .autocapitalization(.none) doesn't work for me in iOS 15. I found .textInputAutocapitalization(.never) does work.
I have the iOS 16 profile installed but Software Update says I have 15.6 and I'm as up to date as I can be. I removed the profile and reinstalled it, yet it still refuses to install iOS 16.
Solved. Some slip of the mouse, perhaps, but the running target had changed to my Mac.
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.
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.
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.