Skip to content

Instantly share code, notes, and snippets.

@delasign
Created October 25, 2024 18:22
Show Gist options
  • Save delasign/01eceafb737f98f666c5a3b8dfb39d38 to your computer and use it in GitHub Desktop.
Save delasign/01eceafb737f98f666c5a3b8dfb39d38 to your computer and use it in GitHub Desktop.
Sample function for converting data into a bookmark with security scope.
import Foundation
func getBookmarkedURL(data: Data) -> URL? {
var isStale = false
do {
let url = try URL(resolvingBookmarkData: data, options: .withSecurityScope, relativeTo: nil, bookmarkDataIsStale: &isStale)
debugPrint("\(DebuggingIdentifiers.actionOrEventInProgress) getBookmarkedURL is Stale ? : \(isStale) | url: \(url).")
guard !isStale, url.startAccessingSecurityScopedResource() else {
debugPrint("\(DebuggingIdentifiers.actionOrEventFailed) getBookmarkedURL - bookmark has expired. Request access again to proceed.")
// TO DO: Mark Bookmark as stale
return nil
}
return url
} catch {
debugPrint(" \(DebuggingIdentifiers.actionOrEventFailed) getBookmarkedURL - error \(error.localizedDescription).")
return nil
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment