SWIFT – Reuse Safety Bookmarks

In my application, I want to access a local file directory with bookmarks of the security range.
As described in the App Sandbox Design Guide, I set the user-specified folder (NSOpenPanel) is stored in a bookmark in a safe range (as NSData).

However, I found that URLByResolvingBookmarkData is no longer available in Swift.

I don’t know how to restart Visit the URL after the application and grant permissions to the directory I selected before. Any ideas?

/// OpenPanel and set the folderPath
var folderPath: NSURL? {
didSet {
do {
let bookmark = try folderPath?.bookmarkDataWithOptions(.SecurityScopeAllowOnlyReadAccess, includingResourceValuesForKeys: nil, relativeToURL: nil)

} catch {
print("Set bookMark fails")
}
}
}

I figured it out with NSUserDefaults.

var userDefault = NSUserDefaults.standardUserDefaults()
var folderPath: NSURL? {
didSet {
do {
let bookmark = try folderPath? .bookmarkDataWithOptions(.SecurityScopeAllowOnlyReadAccess, includingResourceValuesForKeys: nil, relativeToURL: nil)
userDefault.setObject(bookmark, forKey: "bookmark")
} catch let error as NSError {
print("Set Bookmark Fails: \(error.description)")
}
}
}

func applicationDidFinishLaunching(aNotification: NSNotification) {
if let bookmarkData = userDefault.objectForKey("bookmark") as? NSData {
do {
let url = try NSURL.init(byResolvingBookmarkData: bookmarkData, options: .WithoutUI, relativeToURL: nil, bookmarkDataIsStale: nil)
url.startAccessingSecurityScopedResource()
} catch let error as NSError {
print("Bookmark Access Fails: \(error.description)")
}
}
}

In my application In the program, I want to access the local file directory with bookmarks of the security range.
As described in the App Sandbox Design Guide, I store the user-specified folder (NSOpenPanel) in the bookmarks of the security range (as NSData).

However, I found that URLByResolvingBookmarkData is no longer available in Swift.

I don’t know how to access the URL after restarting the application and grant permissions to the directory I previously selected . Any ideas?

/// OpenPanel and set the folderPath
var folderPath: NSURL? {
didSet {
do {
let bookmark = try folderPath?.bookmarkDataWithOptions(.SecurityScopeAllowOnlyReadAccess, includingResourceValuesForKeys: nil, relativeToURL: nil)

} catch {
print("Set bookMark fails")
}
}
}

I used NSUserDefaults to figure it out.

var userDefault = NSUserDefaults.standardUserDefaults()
var folderPath: NSURL? {
didSet {
do {
let bookmark = try folderPath?.bookmarkDataWithOptions(.SecurityScopeAllowOnlyReadAccess, includingResourceValuesForKeys: nil, relativeToURL: nil)
userDefault.setObject(bookmark, forKey: "bookmark")
} catch let error as NSError {
print("Set Bookmark Fails: \(error.description)")
}
}
}

func applicationDidFinishLaunching( aNotification: NSNotification) {
if let bookmarkData = userDefault.objectForKey("bookmark") as? NSData {
do {
let url = try NSURL.init(byResolvingBookmarkData: bookmarkData, options:. WithoutUI, relativeToURL: nil, bookmarkDataIsStale: nil)
url.startAccessingSecurityScopedResource()
} catch let error as NSError {
print("Bookmark Access Fails: \(error.description)")< br /> }
}
}

Leave a Comment

Your email address will not be published.