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文件到我的应用程序的文档文件夹中。
Now I must check if this HTML file exists, so if true, load this HTML file, else load my default html page.
现在我必须检查这个HTML文件是否存在,因此如果是,请加载这个HTML文件,否则将加载我的默认HTML页面。
How I can do that? With NSFileManager
I can't get outside of mainBundle
..
我怎么做呢?使用NSFileManager时,我无法跳出mainBundle。
5 个解决方案
#1
507
Swift 3:
let documentsURL = try! FileManager().url(for: .documentDirectory,
in: .userDomainMask,
appropriateFor: nil,
create: true)
... gives you a file URL of the documents directory. The following checks if there's a file named foo.html:
…提供文档目录的文件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];
#2
13
Apple recommends against relying on the fileExistAtPath: method. It's often better to just try to open a file and deal with the error if the file does not exist.
苹果建议不要依赖fileExistAtPath:方法。如果文件不存在,最好尝试打开一个文件并处理错误。
NSFileManager Class Reference
NSFileManager类引用
Note: Attempting to predicate behavior based on the current state of the file system or a particular file on the file system is not recommended. Doing so can cause odd behavior or race conditions. It's far better to attempt an operation (such as loading a file or creating a directory), check for errors, and handle those errors gracefully than it is to try to figure out ahead of time whether the operation will succeed. For more information on file system race conditions, see “Race Conditions and Secure File Operations” in Secure Coding Guide.
注意:不建议根据文件系统的当前状态或文件系统上的特定文件来谓词行为。这样做会导致奇怪的行为或种族状况。尝试操作(例如加载文件或创建目录)、检查错误并优雅地处理这些错误,要比试图提前判断操作是否成功要好得多。有关文件系统竞态条件的更多信息,请参见安全编码指南中的“竞态条件和安全文件操作”。
Source: Apple Developer API Reference
来源:苹果开发者API参考。
From the secure coding guide.
从安全编码指南。
To prevent this, programs often check to make sure a temporary file with a specific name does not already exist in the target directory. If such a file exists, the application deletes it or chooses a new name for the temporary file to avoid conflict. If the file does not exist, the application opens the file for writing, because the system routine that opens a file for writing automatically creates a new file if none exists. An attacker, by continuously running a program that creates a new temporary file with the appropriate name, can (with a little persistence and some luck) create the file in the gap between when the application checked to make sure the temporary file didn’t exist and when it opens it for writing. The application then opens the attacker’s file and writes to it (remember, the system routine opens an existing file if there is one, and creates a new file only if there is no existing file). The attacker’s file might have different access permissions than the application’s temporary file, so the attacker can then read the contents. Alternatively, the attacker might have the file already open. The attacker could replace the file with a hard link or symbolic link to some other file (either one owned by the attacker or an existing system file). For example, the attacker could replace the file with a symbolic link to the system password file, so that after the attack, the system passwords have been corrupted to the point that no one, including the system administrator, can log in.
为了防止这种情况发生,程序经常检查以确保目标目录中不存在具有特定名称的临时文件。如果存在这样的文件,应用程序将删除它或为临时文件选择一个新名称,以避免冲突。如果文件不存在,应用程序将打开文件进行写入,因为打开文件进行写入的系统例程将自动创建一个不存在的新文件。攻击者,通过持续运行一个程序,创建一个新的临时文件和适当的名称,可以(与持久性和一些运气)创建文件之间的差距当应用程序检查以确保临时文件不存在,当它打开它写作。然后应用程序打开攻击者的文件并写入它(记住,如果有一个文件,系统例程将打开一个现有的文件,并且只有在没有现有文件的情况下才创建一个新文件)。攻击者的文件可能与应用程序的临时文件具有不同的访问权限,因此攻击者可以读取内容。另外,攻击者可能已经打开了文件。攻击者可以用硬链接或符号链接替换其他文件(攻击者或现有系统文件的一个文件)。例如,攻击者可以用到系统密码文件的符号链接替换文件,以便在攻击之后,系统密码已被破坏到包括系统管理员在内的任何人都无法登录的程度。
#3
9
If you set up your file system differently or looking for a different way of setting up a file system and then checking if a file exists in the documents folder heres an another example. also show dynamic checking
如果您以不同的方式设置文件系统,或者寻找另一种设置文件系统的方式,然后检查文件文件夹中是否存在文件,那么下面是另一个例子。也显示动态检查
for (int i = 0; i < numberHere; ++i){
NSFileManager* fileMgr = [NSFileManager defaultManager];
NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString* imageName = [NSString stringWithFormat:@"image-%@.png", i];
NSString* currentFile = [documentsDirectory stringByAppendingPathComponent:imageName];
BOOL fileExists = [fileMgr fileExistsAtPath:currentFile];
if (fileExists == NO){
cout << "DOESNT Exist!" << endl;
} else {
cout << "DOES Exist!" << endl;
}
}
#4
3
check if file exist in side the document/catchimage path :
检查文件/汇流图像路径是否存在文件:
NSString *stringPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
NSString *tempName = [NSString stringWithFormat:@"%@/catchimage/%@.png",stringPath,@"file name"];
NSLog(@"%@",temName);
if([[NSFileManager defaultManager] fileExistsAtPath:temName]){
// ur code here
} else {
// ur code here**
}
#5
3
Swift 2.0
斯威夫特2.0
This is how to check if the file exists using Swift
这是如何检查文件是否使用Swift。
func isFileExistsInDirectory() -> Bool {
let paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
let documentsDirectory: AnyObject = paths[0]
let dataPath = documentsDirectory.stringByAppendingPathComponent("/YourFileName")
return NSFileManager.defaultManager().fileExistsAtPath(dataPath)
}
#1
507
Swift 3:
let documentsURL = try! FileManager().url(for: .documentDirectory,
in: .userDomainMask,
appropriateFor: nil,
create: true)
... gives you a file URL of the documents directory. The following checks if there's a file named foo.html:
…提供文档目录的文件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];
#2
13
Apple recommends against relying on the fileExistAtPath: method. It's often better to just try to open a file and deal with the error if the file does not exist.
苹果建议不要依赖fileExistAtPath:方法。如果文件不存在,最好尝试打开一个文件并处理错误。
NSFileManager Class Reference
NSFileManager类引用
Note: Attempting to predicate behavior based on the current state of the file system or a particular file on the file system is not recommended. Doing so can cause odd behavior or race conditions. It's far better to attempt an operation (such as loading a file or creating a directory), check for errors, and handle those errors gracefully than it is to try to figure out ahead of time whether the operation will succeed. For more information on file system race conditions, see “Race Conditions and Secure File Operations” in Secure Coding Guide.
注意:不建议根据文件系统的当前状态或文件系统上的特定文件来谓词行为。这样做会导致奇怪的行为或种族状况。尝试操作(例如加载文件或创建目录)、检查错误并优雅地处理这些错误,要比试图提前判断操作是否成功要好得多。有关文件系统竞态条件的更多信息,请参见安全编码指南中的“竞态条件和安全文件操作”。
Source: Apple Developer API Reference
来源:苹果开发者API参考。
From the secure coding guide.
从安全编码指南。
To prevent this, programs often check to make sure a temporary file with a specific name does not already exist in the target directory. If such a file exists, the application deletes it or chooses a new name for the temporary file to avoid conflict. If the file does not exist, the application opens the file for writing, because the system routine that opens a file for writing automatically creates a new file if none exists. An attacker, by continuously running a program that creates a new temporary file with the appropriate name, can (with a little persistence and some luck) create the file in the gap between when the application checked to make sure the temporary file didn’t exist and when it opens it for writing. The application then opens the attacker’s file and writes to it (remember, the system routine opens an existing file if there is one, and creates a new file only if there is no existing file). The attacker’s file might have different access permissions than the application’s temporary file, so the attacker can then read the contents. Alternatively, the attacker might have the file already open. The attacker could replace the file with a hard link or symbolic link to some other file (either one owned by the attacker or an existing system file). For example, the attacker could replace the file with a symbolic link to the system password file, so that after the attack, the system passwords have been corrupted to the point that no one, including the system administrator, can log in.
为了防止这种情况发生,程序经常检查以确保目标目录中不存在具有特定名称的临时文件。如果存在这样的文件,应用程序将删除它或为临时文件选择一个新名称,以避免冲突。如果文件不存在,应用程序将打开文件进行写入,因为打开文件进行写入的系统例程将自动创建一个不存在的新文件。攻击者,通过持续运行一个程序,创建一个新的临时文件和适当的名称,可以(与持久性和一些运气)创建文件之间的差距当应用程序检查以确保临时文件不存在,当它打开它写作。然后应用程序打开攻击者的文件并写入它(记住,如果有一个文件,系统例程将打开一个现有的文件,并且只有在没有现有文件的情况下才创建一个新文件)。攻击者的文件可能与应用程序的临时文件具有不同的访问权限,因此攻击者可以读取内容。另外,攻击者可能已经打开了文件。攻击者可以用硬链接或符号链接替换其他文件(攻击者或现有系统文件的一个文件)。例如,攻击者可以用到系统密码文件的符号链接替换文件,以便在攻击之后,系统密码已被破坏到包括系统管理员在内的任何人都无法登录的程度。
#3
9
If you set up your file system differently or looking for a different way of setting up a file system and then checking if a file exists in the documents folder heres an another example. also show dynamic checking
如果您以不同的方式设置文件系统,或者寻找另一种设置文件系统的方式,然后检查文件文件夹中是否存在文件,那么下面是另一个例子。也显示动态检查
for (int i = 0; i < numberHere; ++i){
NSFileManager* fileMgr = [NSFileManager defaultManager];
NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString* imageName = [NSString stringWithFormat:@"image-%@.png", i];
NSString* currentFile = [documentsDirectory stringByAppendingPathComponent:imageName];
BOOL fileExists = [fileMgr fileExistsAtPath:currentFile];
if (fileExists == NO){
cout << "DOESNT Exist!" << endl;
} else {
cout << "DOES Exist!" << endl;
}
}
#4
3
check if file exist in side the document/catchimage path :
检查文件/汇流图像路径是否存在文件:
NSString *stringPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
NSString *tempName = [NSString stringWithFormat:@"%@/catchimage/%@.png",stringPath,@"file name"];
NSLog(@"%@",temName);
if([[NSFileManager defaultManager] fileExistsAtPath:temName]){
// ur code here
} else {
// ur code here**
}
#5
3
Swift 2.0
斯威夫特2.0
This is how to check if the file exists using Swift
这是如何检查文件是否使用Swift。
func isFileExistsInDirectory() -> Bool {
let paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
let documentsDirectory: AnyObject = paths[0]
let dataPath = documentsDirectory.stringByAppendingPathComponent("/YourFileName")
return NSFileManager.defaultManager().fileExistsAtPath(dataPath)
}