3

I enabled Document Types to import or copy files from other apps to my application. I have some questions :

1- Where should create the method of moving files form Inbox to Document directory ? is this the right place ? func applicationWillEnterForeground(_ application: UIApplication)

2- On first view controller I am getting files from Document directory :

  func getFileListByDate() -> [String]? {

        let directory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
        if let urlArray = try? FileManager.default.contentsOfDirectory(at: directory,
                                                                       includingPropertiesForKeys: [.contentModificationDateKey],
                                                                       options:.skipsHiddenFiles) {

            return urlArray.map { url in
                (url.lastPathComponent, (try? url.resourceValues(forKeys: [.contentModificationDateKey]))?.contentModificationDate ?? Date.distantPast)
                }
                .sorted(by: { $0.1 > $1.1 }) // sort descending modification dates
                .map { $0.0 } // extract file names

        } else {
            return nil
        }

    }

But when a file imports to my app there is Inbox folder(item) in my table view , how can I automatically move files from Inbox to Document directory and remove Inbox folder ?

iOS.Lover
  • 5,923
  • 21
  • 90
  • 162

1 Answers1

6

If your app needs to open a file coming from another App you need to implement delegate method

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {

and move the url to the folder of your choice inside your App.

let url = url.standardizedFileURL  // this will strip out the private from your url
// if you need to know which app is sending the file or decide if you will open in place or not you need to check the options  
let openInPlace = options[.openInPlace] as? Bool == true
let sourceApplication = options[.sourceApplication] as? String
let annotation = options[.annotation] as? [String: Any]
// checking the options info
print("openInPlace:", openInPlace)
print("sourceApplication:", sourceApplication ?? "")
print("annotation:", annotation ?? "")

Moving the file out of the inbox to your destination URL in your case the documents directory appending the url.lastPathComponent:

do {
    try FileManager.default.moveItem(at: url, to: destinationURL)
    print(url.path)
    print("file moved from:", url, "to:", destinationURL)

} catch {
    print(error)
    return false
}

return true
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
  • Thank you, I will try it , but one question is how can I remove inbox folder ? – iOS.Lover Oct 05 '17 at 13:26
  • 1
    @Mc.Lover You can't. But it doesn't mean that you need to show it to the user. You can filter your array before displaying it – Leo Dabus Oct 05 '17 at 13:26
  • So is there any way to hide it ? because I am getting files from Documents folder by this method `getFileListByDate` and this show me both folders and files I need to show only files – iOS.Lover Oct 05 '17 at 13:28
  • If you just need to display files just filter all urls that are directory – Leo Dabus Oct 05 '17 at 13:29
  • I can't see how you are using the strings returned by your method. If the count doesn't matter just filter != "Inbox" https://stackoverflow.com/questions/27878798/remove-specific-array-element-equal-to-string-swift/27878889#27878889 – Leo Dabus Oct 05 '17 at 13:31
  • I suggest you should use an array of url instead of using paths – Leo Dabus Oct 05 '17 at 13:33
  • @Mc.Lover this have nothing to do with the original question. Just open a new question and post the link here. I will gladly help you filtering your inbox out of your table view – Leo Dabus Oct 05 '17 at 13:36
  • @Mc.Lover I suggest you revert your question to the original post – Leo Dabus Oct 05 '17 at 13:36
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/156022/discussion-between-mc-lover-and-leo-dabus). – iOS.Lover Oct 05 '17 at 13:44