NSFileManager category
Each method in the NSFileManager category corresponds to a method in the NSFileManager class, but provides a secure version of the functionality. For more information about the functionality and usage, see NSFileManager in developer.apple.com.
| NOTE: | The category methods return an NSError object. The methods set the properties on the object as described in NSError objects that secure Objective-C methods return. |
The following table shows each added method and its corresponding method in NSFileManager.
|
Method in category |
Corresponding method in NSFileManager |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Example:
The following example shows how to move a secure file to a new location. Specifically, the example:
| 1. | Creates a secure file. |
| 2. | Writes the contents of the unsecured file /etc/group into the secure file. |
| 3. | Moves the secure file to a new location using the NSFileManager+ACSecureData category methods. |
| NOTE: | For brevity, the example does not include error handling. |
- (void)NSFileManagerCategoryExample
{
NSError *error;
// Read the contents of /etc/group.
NSData *etcGroupData = [NSData dataWithContentsOfFile:@"/etc/group"];
// Create a secure file with the contents of /etc/group.
NSString *secureFileName = @"/tmp/secureFile";
[[NSFileManager defaultManager] createSecureFileAtPath:secureFileName
contents:etcGroupData attributes:nil];
// Move the newly created secure file to a new location.
// First, create the source and destination file URLs.
NSString *anotherSecureFileName = @"/tmp/anotherSecureFile";
NSURL *sourceURL = [NSURL fileURLWithPath:secureFileName];
NSURL *destinationURL = [NSURL fileURLWithPath:anotherSecureFileName];
// Move the secure file.
[[NSFileManager defaultManager] moveSecureFileAtURL:sourceURL
toURL:destinationURL error:&error];
// Note: The following line incorrectly moves a secure file.
// Mixing regular and secure file I/O on the same file can result
// in corrupted data.
// DO NOT USE.
// [[NSFileManager defaultManager] moveItemAtPath:sourceURL toPath:destinationURL
error:&error];
}