2026-09-19 15:52:16 +02:00
|
|
|
//
|
|
|
|
|
// FileBookmarkService.swift
|
2026-09-20 15:05:19 +02:00
|
|
|
// KeeVault
|
2026-09-19 15:52:16 +02:00
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
|
|
|
|
|
/// Persists a security-scoped bookmark for the user's KDBX file in the shared App Group.
|
|
|
|
|
public final class FileBookmarkService {
|
|
|
|
|
private static let key = "kdbxBookmark"
|
|
|
|
|
private let defaults: UserDefaults
|
|
|
|
|
|
2026-09-20 15:05:19 +02:00
|
|
|
public init(appGroup: String = "group.org.antiloop222.keevault") {
|
2026-09-19 15:52:16 +02:00
|
|
|
defaults = UserDefaults(suiteName: appGroup) ?? .standard
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-19 16:00:37 +02:00
|
|
|
// `.withSecurityScope` is a macOS-only bookmark-creation option; iOS grants scoped
|
|
|
|
|
// access automatically once the user picks a file, without that flag.
|
|
|
|
|
private static var creationOptions: URL.BookmarkCreationOptions {
|
|
|
|
|
#if os(macOS)
|
|
|
|
|
return .withSecurityScope
|
|
|
|
|
#else
|
|
|
|
|
return []
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static var resolutionOptions: URL.BookmarkResolutionOptions {
|
|
|
|
|
#if os(macOS)
|
|
|
|
|
return .withSecurityScope
|
|
|
|
|
#else
|
|
|
|
|
return []
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-19 15:52:16 +02:00
|
|
|
public func save(url: URL) throws {
|
2026-09-20 12:39:45 +02:00
|
|
|
// For File Provider-backed URLs (e.g. iCloud Drive), creating a bookmark without
|
|
|
|
|
// an active security scope produces one that resolves later with
|
|
|
|
|
// NSCocoaErrorDomain Code=4 ("The file doesn't exist"), even immediately after picking.
|
|
|
|
|
let accessing = url.startAccessingSecurityScopedResource()
|
|
|
|
|
defer { if accessing { url.stopAccessingSecurityScopedResource() } }
|
2026-09-19 15:52:16 +02:00
|
|
|
let data = try url.bookmarkData(
|
2026-09-19 16:00:37 +02:00
|
|
|
options: Self.creationOptions,
|
2026-09-19 15:52:16 +02:00
|
|
|
includingResourceValuesForKeys: nil,
|
|
|
|
|
relativeTo: nil
|
|
|
|
|
)
|
|
|
|
|
defaults.set(data, forKey: Self.key)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns the resolved URL, starting security access. Caller must call `stopAccess(url:)` when done.
|
|
|
|
|
public func resolveURL() throws -> URL {
|
|
|
|
|
guard let data = defaults.data(forKey: Self.key) else { throw BookmarkError.notFound }
|
|
|
|
|
var isStale = false
|
|
|
|
|
let url = try URL(
|
|
|
|
|
resolvingBookmarkData: data,
|
2026-09-19 16:00:37 +02:00
|
|
|
options: Self.resolutionOptions,
|
2026-09-19 15:52:16 +02:00
|
|
|
relativeTo: nil,
|
|
|
|
|
bookmarkDataIsStale: &isStale
|
|
|
|
|
)
|
|
|
|
|
if isStale {
|
2026-09-19 16:00:37 +02:00
|
|
|
let fresh = try url.bookmarkData(options: Self.creationOptions, includingResourceValuesForKeys: nil, relativeTo: nil)
|
2026-09-19 15:52:16 +02:00
|
|
|
defaults.set(fresh, forKey: Self.key)
|
|
|
|
|
}
|
|
|
|
|
guard url.startAccessingSecurityScopedResource() else { throw BookmarkError.accessDenied }
|
|
|
|
|
return url
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public func stopAccess(url: URL) {
|
|
|
|
|
url.stopAccessingSecurityScopedResource()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public func clear() {
|
|
|
|
|
defaults.removeObject(forKey: Self.key)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public var hasBookmark: Bool {
|
|
|
|
|
defaults.data(forKey: Self.key) != nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public enum BookmarkError: Error {
|
|
|
|
|
case notFound
|
|
|
|
|
case accessDenied
|
|
|
|
|
}
|