簡體   English   中英

iOS,將文件從收件箱文件夾復制到文檔路徑

[英]iOS , Copying files from Inbox folder to Document path

我啟用了文檔類型以將文件從其他應用程序導入或復制到我的應用程序。 我有一些疑問 :

1-在哪里應該創建將文件從收件箱移到文檔目錄的方法? 這是對的地方嗎? func applicationWillEnterForeground(_ application: UIApplication)

2-在第一個視圖控制器上,我從Document目錄獲取文件:

  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
        }

    }

但是,當文件導入到我的應用程序時,表視圖中有Inbox文件夾(項目),如何自動將文件從Inbox移到Document目錄並刪除Inbox文件夾?

如果您的應用需要打開另一個應用的文件,則需要實現委托方法

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

並將網址移到您的應用內您選擇的文件夾中。

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 ?? "")

如果是在文件目錄中附加url.lastPathComponent的情況下,將文件從收件箱移至目標URL:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM