Available on Android, iOS, macOS, and JVM targets
The Problem: Losing File Access
Modern operating systems use security measures like sandboxing, which means your app can lose access to files selected by the user once it restarts. A standard file path might become invalid. This is especially true on Android (for files outside your app’s private storage) and on sandboxed apps on macOS and iOS.The Solution: Bookmark Data
BookmarkData is a feature that creates a persistent, secure reference to a file. You can save this reference and use it later to reliably regain access to the file, even after your app has been closed and reopened. FileKit handles the complex platform-specific implementations for you.
The Basic Workflow
The process involves two main steps: creating and saving a bookmark, and later loading and resolving it.Complete Example
Here’s a more complete, practical example using a simple object to manage the bookmark.Platform-Specific Behavior
FileKit abstracts away the details, but here’s what happens on each platform:-
Android: For standard file paths, the path itself is stored. For
content://URIs from the system picker, FileKit requests persistent URI permissions and stores the URI string. This ensures long-term access. -
iOS & macOS: Uses the native security-scoped bookmark system. This is crucial for sandboxed apps. FileKit automatically starts and stops access to the security-scoped resource when you use the restored
PlatformFile. - JVM: Stores the file’s absolute path. This is usually sufficient for desktop apps which are not typically sandboxed.
Handling Invalid Bookmarks
A bookmark is not a guarantee. It can become invalid if the original file is deleted, moved, or if permissions change.Always handle restoration failures gracefully. A bookmark can become invalid if:
- The user moves or deletes the file.
- The user revokes file permissions for your app.
- System security policies change.
- The app is uninstalled and reinstalled (on some platforms).