首页 > 解决方案 > 文件管理器检查 fileExists 是否在 iOS 13 中不起作用

问题描述

我有一个将图像存储到 documentDirectory 的应用程序。但是,每当我尝试检查文件是否存在于其路径中时,我总是会得到错误的响应。这在 iOS 13 之前从来都不是问题,但我根本无法加载图像,没有错误消息。

我像这样创建我的文件路径:

func cachePathWithName(name: String) -> String {
    let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString
    let cachesPath: String = paths as String
    let cachePath = cachesPath.stringByAppendingPathComponent(path: name)
    createPathIfNecessary(path: cachesPath)
    createPathIfNecessary(path: cachePath)
    return cachePath
}

func getFilePathForURL(url: URL, folderName: String) -> String {
    return cachePathWithName(name: folderName).stringByAppendingPathComponent(path: "\(url.hashValue)")
}

let filePath = getFilePathForURL(url: url1, folderName: "TILE_CACHE")

这就是我检查我的文件是否存在于路径中的方式:

if file.fileExists(atPath: filePath) {

}

标签: iosswift

解决方案


试试这个代码

let documentsURL = try! FileManager().url(for: .documentDirectory,
                                                      in: .userDomainMask,
                                                      appropriateFor: nil,
                                                      create: true)

            fileURL = documentsURL.appendingPathComponent(Your File Name)
            if fileURL != nil{
                let fileExists = FileManager().fileExists(atPath: (fileURL?.path)!)
                print(fileExists)
                if fileExists == true{
                    //File is Already in Local Storage
                }
                else{
                    //File is not in Local Storage
                }
            }

推荐阅读