Bookmark
A Swift wrapper for URL bookmarks which allow a file to be located regardless of whether it is moved or renamed.
This class wraps Swift’s URL bookmark
functionality. See Apple’s documentation for further information.
A bookmark can be stored (eg. on disk, in a database etc.) and reloaded and it will be able to locate the original target for the bookmark, even it has been moved or renamed.
A bookmark is an opaque data structure, enclosed in a Data
object, that describes the location of a file. Whereas path and file reference URLs are potentially fragile between launches of your app, a bookmark can usually be used to re-create a URL to a file even in cases where the file was moved or renamed.
Some information links :-
- Locating Files Using Bookmarks
- Enabling Security-Scoped Bookmark and URL Access
- Bookmarks and Security Scope
Usage
Create and use a bookmark
// The original file url
let originalURL = URL(targetFileURL: ...)!
// Create a bookmark
let bookmark = try Bookmark(targetFileURL: originalURL)
// Access to the raw bookmark data
let bookmarkData = bookmark.bookmarkData
try bookmark.usingTargetURL { targetURL in
// Do something with the targetURL which is the original URL
}
// ... Somewhere in here, the original url file is moved or renamed ...
try bookmark.usingTargetURL { targetURL in
// Do something with the targetURL (which will correctly point to the new URL location)
}
Save/Load bookmark data
// The original file url
let originalURL = URL(targetFileURL: ...)!
// Create a bookmark
let bookmark = try Bookmark(targetFileURL: originalURL)
// Grab out the raw bookmark data
let storableData = bookmark.bookmarkData
// ...Save the bookmark data for later use, eg. in CoreData or in a database...
// Load the bookmark data back out from the storage medium...
let savedBookmarkData = <load bookmark data from somewhere>
// ... and recreate the Bookmark object from the data
let existingBookmark = try Bookmark(bookmarkData: savedBookmarkData)
// Use the loaded bookmark
try existingBookmark.usingTargetURL { targetURL in
// ...Do something with the targetURL...
}
License
MIT. Use it for anything you want, just attribute my work if you do. Let me know if you do use it somewhere, I’d love to hear about it!
MIT License
Copyright (c) 2022 Darren Ford
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.