             throw SecurityScopedBookMarkError.bookMarkFailed(message: "\(error)")
         }
     }
-    
-    
+
+    /**
+     Starts acessing to the securityScopedResource
+
+     - parameter scopedUrl: the scopedUrl
+     */
+    fileprivate func _startAccessingToSecurityScopedResourceAtURL(_ scopedUrl: URL) {
+        if self._activeSecurityBookmarks.index(of: scopedUrl)==nil {
+            let _ = scopedUrl.startAccessingSecurityScopedResource()
+            self._activeSecurityBookmarks.append(scopedUrl)
+        }
+    }
+
+
+    /**
+     Stops to access to securityScopedResource
+
+     - parameter url: the url
+     */
+    fileprivate func _stopAccessingToSecurityScopedResourceAtURL(_ scopedUrl: URL) {
+        if let idx=self._activeSecurityBookmarks.index(of: scopedUrl) {
+            scopedUrl.stopAccessingSecurityScopedResource()
+            self._activeSecurityBookmarks.remove(at: idx)
+        }
+
+    }
 }

    import UIKit
#endif


extension BartlebyDocument {

    public enum SecurityScopedBookMarkError: Error {
        // Bookmarking
        case bookMarkFailed(message:String)
        // Scoped URL
        case getScopedURLRessourceFailed(message:String)
        case bookMarkIsStale
    }
    // MARK: Security-Scoped Bookmarks support


    // https://developer.apple.com/library/mac/documentation/Security/Conceptual/AppSandboxDesignGuide/AppSandboxInDepth/AppSandboxInDepth.html#//apple_ref/doc/uid/TP40011183-CH3-SW16

    // After an explicit user intent
    // #1 scopedURL=getSecurizedURL(url, ... )
    // #2 startAccessingToSecurityScopedResourceAtURL(scopedURL)
    // ... use the resource
    // #3 stopAccessingToSecurityScopedResourceAtURL(scopedURL)


    /**

     Returns the securized URL
     If the Securirty scoped Bookmark do not exists, it creates one.
     You must call this method after a user explicit intent (NSOpenPanel ...)
     You cannot get security scoped bookmark for an arbritrary URL.

     NOTE : Don't forget that you must call startAccessingToSecurityScopedResourceAtURL(scopedURL) as soon as you use the URL, and stopAccessingToSecurityScopedResourceAtURL(scopedURL) as soon as you can release the resource.



     - parameter url:             the URL to be accessed
     - parameter appScoped:       appScoped description
     - parameter documentfileURL: documentfileURL description

     - throws: throws various exception (on creation, and or resolution)

     - returns: the securized URL
     */
    public func getSecurizedURL(_ url: URL, appScoped: Bool=false, documentfileURL: URL?=nil) throws ->URL {
        if self.securityScopedBookmarkExits(url, appScoped: false, documentfileURL:nil)==false {
            return try self.bookmarkURL(url, appScoped: false, documentfileURL:nil)
        } else {
            return try self.getSecurityScopedURLFrom(url, appScoped: false, documentfileURL:nil)
        }
    }

    /**
     Deletes a security scoped bookmark (e.g : when you delete a resource)

     - parameter url:             url description
     - parameter appScoped:       appScoped description
     - parameter documentfileURL: documentfileURL description
     */
    public func deleteSecurityScopedBookmark(_ url: URL, appScoped: Bool=false, documentfileURL: URL?=nil) {
        let key=_getBookMarkKeyFor(url, appScoped: appScoped, documentfileURL: documentfileURL)
        self.metadata.URLBookmarkData.removeValue(forKey: key)

    }

    /**
     Starts acessing to the securityScopedResource

     - parameter scopedUrl: the scopedUrl
     */
    public func startAccessingToSecurityScopedResourceAtURL(_ scopedUrl: URL) {
        let _ = scopedUrl.startAccessingSecurityScopedResource()
        if self._activeSecurityBookmarks.index(of: scopedUrl)==nil {
            self._activeSecurityBookmarks.append(scopedUrl)
        }
    }


    /**
     Stops to access to securityScopedResource

     - parameter url: the url
     */
    public func stopAccessingToSecurityScopedResourceAtURL(_ scopedUrl: URL) {
        scopedUrl.stopAccessingSecurityScopedResource()
        if let idx=self._activeSecurityBookmarks.index(of: scopedUrl) {
            self._activeSecurityBookmarks.remove(at: idx)
        }

    }



    /**
     Stops to access to all the security Scoped Resources
     */
    public func stopAccessingAllSecurityScopedResources() {
        while  let key = self._activeSecurityBookmarks.first {
            self.stopAccessingToSecurityScopedResourceAtURL(key as URL)
        }
    }


    //MARK: Advanced interface (can be used in special context)

    /**
     Creates and store for future usage a security scoped bookmark.

     - parameter url:       the url
     - parameter appScoped: if set to true it is an app-scoped bookmark else a document-scoped bookmark
     - parameter documentfileURL :  the document file URL if not app scoped (you can create a bookmark for another document)

     - returns: return the security scoped resource URL
     */
    public func bookmarkURL(_ url: URL, appScoped: Bool=false, documentfileURL: URL?=nil) throws -> URL {
        var shareData = try self._createDataFromBookmarkForURL(url, appScoped:appScoped, documentfileURL:documentfileURL)
        // Encode the bookmark data as a Base64 string.
        shareData=shareData.base64EncodedData(options: .endLineWithCarriageReturn)
        let stringifyedData=String(data: shareData, encoding: Default.STRING_ENCODING)
        let key=_getBookMarkKeyFor(url, appScoped: appScoped, documentfileURL: documentfileURL)
        self.metadata.URLBookmarkData[key]=stringifyedData as AnyObject?
        self.hasChanged()
        return try getSecurityScopedURLFrom(url)

    }

    fileprivate func _getBookMarkKeyFor(_ url: URL, appScoped: Bool=false, documentfileURL: URL?=nil) -> String {
        let path=url.path
        return CryptoHelper.hashString("\(path)-\((appScoped ? "YES" : "NO" ))-\(documentfileURL?.path ?? Default.NO_PATH ))")
    }

    /**

    }


    internal func _securityScopedBookmarkExits(_ url: URL, appScoped: Bool=false) -> Bool {
        let key=_getBookMarkKeyFor(url, appScoped: appScoped)
        let result=self.metadata.URLBookmarkData.keys.contains(key)
        return result
    }



    fileprivate func _createDataFromBookmarkForURL(_ fileURL: URL, appScoped: Bool=false) throws -> Data {
        do {
            #if os(OSX)
                let data = try (fileURL as URL).bookmarkData(options: URL.BookmarkCreationOptions.withSecurityScope,
                                                               includingResourceValuesForKeys:nil,
                                                               relativeTo: appScoped ? nil : ( self.fileURL ) )
            #else
                let data =   try fileURL.bookmarkData(options: URL.BookmarkCreationOptions.suitableForBookmarkFile,
                                                      includingResourceValuesForKeys: nil,
                                                      relativeTo: appScoped ? nil : ( self.fileURL ))

            #endif
            // Extract of : https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/URL_Class/index.html#//apple_ref/occ/instm/URL/bookmarkDataWithOptions:includingResourceValuesForKeys:relativeToURL:error:
