ios - 如何检查文档文件夹中是否存在文件?

标签 ios iphone xcode webview nsfilemanager

我有一个应用程序内购买的应用程序,当用户购买东西时,将一个 html 文件下载到我的应用程序的文档文件夹中。

现在我必须检查这个 HTML 文件是否存在,如果是,则加载这个 HTML 文件,否则加载我的默认 html 页面。

我该怎么做?使用 NSFileManager 我无法离开 mainBundle..

最佳答案

swift 3:

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

... 为您提供文档目录的文件 URL。以下检查是否有名为 foo.html 的文件:

let fooURL = documentsURL.appendingPathComponent("foo.html")
let fileExists = FileManager().fileExists(atPath: fooURL.path)

objective-C :

NSString* documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];

NSString* foofile = [documentsPath stringByAppendingPathComponent:@"foo.html"];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:foofile];

关于ios - 如何检查文档文件夹中是否存在文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1638834/

相关文章:

ios - 如何包含和设置启动屏幕状态栏颜色?

iphone - Objective C - NSArray 和 For 循环结构

ios - 响应式 3D Touch

ios - 当用户隐藏应用程序或关闭设备时,swiftui 中的自定义计时器停止在后台获取中计数

ios - UILabel 自动换行+多行

iphone - 暂停 UIImageview 动画

ios - 在单个 UITableView 中填充两种类型的 UITableViewCell

objective-c - 在 Swift 中,您将如何编写 scrollViewWillEndDragging withVelocity targetContentOffset 方法?

ios - 如何以编程方式在 UITableViewCell 中添加 UIView?

ios - 删除特定字符后的空格