瀏覽代碼

Modify Document Picker

Marino Faggiana 8 年之前
父節點
當前提交
822600eeb9
共有 2 個文件被更改,包括 24 次插入17 次删除
  1. 13 9
      Picker/DocumentPickerViewController.swift
  2. 11 8
      PickerFileProvider/FileProvider.swift

+ 13 - 9
Picker/DocumentPickerViewController.swift

@@ -105,7 +105,7 @@ class DocumentPickerViewController: UIDocumentPickerExtensionViewController, CCN
             if (localServerUrl == nil) {
             
                 localServerUrl = CCUtility.getHomeServerUrlActiveUrl(activeUrl, typeCloud: typeCloud)
-                
+                                
             } else {
                 
                 self.navigationItem.title = titleFolder
@@ -397,19 +397,24 @@ class DocumentPickerViewController: UIDocumentPickerExtensionViewController, CCN
         switch selector {
             
         case selectorLoadFileView :
+            
+            let sourceUrl = URL(string: "file://\(directoryUser!)/\(fileID!)".addingPercentEncoding(withAllowedCharacters: .urlPathAllowed)!)!
+            let destinationUrl : URL! = appGroupContainerURL()?.appendingPathComponent(metadata!.fileNamePrint!)
                 
             do {
-                
-                try FileManager.default.moveItem(atPath: "\(directoryUser!)/\(fileID!)", toPath: "\(directoryUser!)/\(metadata!.fileNamePrint!)")
-                    
+                try FileManager.default.removeItem(at: destinationUrl)
+            } catch _ {
+                print("file do not exists")
+            }
+
+            do {
+                try FileManager.default.copyItem(at: sourceUrl, to: destinationUrl)
             } catch let error as NSError {
-        
                 print(error)
             }
                 
-            let url = URL(string: "file://\(directoryUser!)/\(metadata!.fileNamePrint!)".addingPercentEncoding(withAllowedCharacters: .urlPathAllowed)!)
-            self.dismissGrantingAccess(to: url)
-            
+            self.dismissGrantingAccess(to: destinationUrl)
+                      
         case selectorLoadPlist :
             
             CCCoreData.downloadFilePlist(metadata, activeAccount: activeAccount, activeUrl: activeUrl, typeCloud: typeCloud, directoryUser: directoryUser)
@@ -419,7 +424,6 @@ class DocumentPickerViewController: UIDocumentPickerExtensionViewController, CCN
             
             print("selector : \(selector!)")
             tableView.reloadData()
-            
         }
     }
  

+ 11 - 8
PickerFileProvider/FileProvider.swift

@@ -63,18 +63,21 @@ class FileProvider: NSFileProviderExtension {
     }
     
     override func startProvidingItem(at url: URL, completionHandler: ((_ error: Error?) -> Void)?) {
-        // Should ensure that the actual file is in the position returned by URLForItemWithIdentifier, then call the completion handler
         
-        // TODO: get the contents of file at <url> from model
-        let fileData = NSData()
+        guard let fileData = try? Data(contentsOf: url) else {
+            // NOTE: you would generate an NSError to supply to the completionHandler
+            // here however that is outside of the scope for this tutorial
+            completionHandler?(nil)
+            return
+        }
         
         do {
-            _ = try fileData.write(to: url, options: [])
-        } catch {
-            // Handle error
+            _ = try fileData.write(to: url, options: NSData.WritingOptions())
+            completionHandler?(nil)
+        } catch let error as NSError {
+            print("error writing file to URL")
+            completionHandler?(error)
         }
-        
-        completionHandler?(nil);
     }
     
     override func itemChanged(at url: URL) {