123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- import UIKit
- class FileProvider: NSFileProviderExtension {
- var fileCoordinator: NSFileCoordinator {
- let fileCoordinator = NSFileCoordinator()
- fileCoordinator.purposeIdentifier = self.providerIdentifier
- return fileCoordinator
- }
-
- override init() {
- super.init()
-
- self.fileCoordinator.coordinate(writingItemAt: self.documentStorageURL, options: [], error: nil, byAccessor: { newURL in
-
- do {
- try FileManager.default.createDirectory(at: newURL, withIntermediateDirectories: true, attributes: nil)
- } catch {
-
- }
- })
- }
-
- override func providePlaceholder(at url: URL, completionHandler: ((_ error: Error?) -> Void)?) {
-
- let fileName = url.lastPathComponent
-
- let placeholderURL = NSFileProviderExtension.placeholderURL(for: self.documentStorageURL.appendingPathComponent(fileName))
-
-
- let fileSize = 0
- let metadata = [AnyHashable(URLResourceKey.fileSizeKey): fileSize]
- do {
- try NSFileProviderExtension.writePlaceholder(at: placeholderURL, withMetadata: metadata)
- } catch {
-
- }
-
- completionHandler?(nil)
- }
-
- override func startProvidingItem(at url: URL, completionHandler: ((_ error: Error?) -> Void)?) {
-
- guard let fileData = try? Data(contentsOf: url) else {
-
-
- completionHandler?(nil)
- return
- }
-
- do {
- _ = try fileData.write(to: url, options: NSData.WritingOptions())
- completionHandler?(nil)
- } catch let error as NSError {
- print("error writing file to URL")
- completionHandler?(error)
- }
- }
-
- override func itemChanged(at url: URL) {
-
-
-
- NSLog("Item changed at URL %@", url as NSURL)
- }
-
- override func stopProvidingItem(at url: URL) {
-
-
-
- do {
- _ = try FileManager.default.removeItem(at: url)
- } catch {
-
- }
- self.providePlaceholder(at: url, completionHandler: { error in
-
- })
- }
- }
|