如何检查Documents文件夹中是否存在文件? [英] How to check if a file exists in Documents folder?
本文介绍了如何检查Documents文件夹中是否存在文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个应用程序内购买的应用程序,当用户购买东西时,将一个html文件下载到我的应用程序的Documents文件夹中。
I have an application with In-App Purchase, that when the user buy something, download one html file into the Documents folder of my app.
现在我必须检查这个HTML文件是否存在,如果是,则加载此HTML文件,否则加载我的默认html页面。
Now I must check if this HTML file exists, so if true, load this HTML file, else load my default html page.
我该怎么做?使用 NSFileManager
我无法超出 mainBundle
..
How I can do that? With NSFileManager
I can't get outside of mainBundle
..
推荐答案
Swift 3:
Swift 3:
let documentsURL = try! FileManager().url(for: .documentDirectory,
in: .userDomainMask,
appropriateFor: nil,
create: true)
...为您提供文档目录的文件URL。以下检查是否存在名为foo.html的文件:
... gives you a file URL of the documents directory. The following checks if there's a file named foo.html:
let fooURL = documentsURL.appendingPathComponent("foo.html")
let fileExists = FileManager().fileExists(atPath: fooURL.path)
Objective-C :
Objective-C:
NSString* documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString* foofile = [documentsPath stringByAppendingPathComponent:@"foo.html"];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:foofile];
这篇关于如何检查Documents文件夹中是否存在文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文