Saturday, June 1, 2013

NSData NSDataWritingFileProtectionComplete example in Objective C (iOS).


NSData NSDataWritingFileProtectionComplete

NSDataWritingOptions
Options for methods used to write NSData objects.

enum {
NSDataWritingAtomic = 1UL << 0,
NSDataWritingWithoutOverwriting = 1UL << 1,
NSDataWritingFileProtectionNone = 0x10000000,
NSDataWritingFileProtectionComplete = 0x20000000,
NSDataWritingFileProtectionCompleteUnlessOpen = 0x30000000,
NSDataWritingFileProtectionCompleteUntilFirstUserAuthentication = 0x40000000,
NSDataWritingFileProtectionMask = 0xf0000000,
};
typedef NSUInteger NSDataWritingOptions;
Constants
NSDataWritingAtomic
A hint to write data to an auxiliary file first and then exchange the files. This option is equivalent to using a write method taking the parameter atomically:YES.
NSDataWritingWithoutOverwriting
Hint to return prevent overwriting an existing file. Cannot be combined with NSDataWritingAtomic.
NSDataWritingFileProtectionNone
A hint to set the content protection attribute of the file when writing it out. In this case, the file is not stored in an encrypted format and may be accessed at boot time and while the device is unlocked.
Available in iOS 4.0 and later.
Declared in NSData.h.
NSDataWritingFileProtectionComplete
A hint to set the content protection attribute of the file when writing it out. In this case, the file is stored in an encrypted format and may be read from or written to only while the device is unlocked. At all other times, attempts to read and write the file result in failure.
NSDataWritingFileProtectionCompleteUnlessOpen
A hint to set the content protection attribute of the file when writing it out. In this case, the file cannot be opened for reading or writing when the device is locked, although new files can be created with this class. If one of these files is open when the device is locked, reading and writing are still allowed.
NSDataWritingFileProtectionCompleteUntilFirstUserAuthentication
A hint to set the content protection attribute of the file when writing it out. In this case, the file can be read or written to while the device is locked, but while it is booting up, they have protection equivalent to NSDataWritingFileProtectionComplete.
NSDataWritingFileProtectionMask
A mask to use when determining the file protection options assigned to the data.

NSData NSDataWritingFileProtectionComplete example.
NSData * data = // Your decrypted file data.
NSString * fileName = // Whatever you want to name your file.
NSString * path = [NSTemporaryDirectory() stringByAppendingPathComponent:fileName];
NSURL * url = [NSURL URLWithString:path];
NSError * error = nil;

BOOL success = [data writeToURL:url
                        options:NSDataWritingFileProtectionComplete
                          error:&error];
if (success) {
    // Give the URL to Quick Look.
}
else {
    // An error happened. See the 'error' object for the details.
}

Example of [NSData NSDataWritingFileProtectionComplete].
    NSError *error = nil;
    [fileData writeToFile:filePath options:NSDataWritingFileProtectionComplete error:&error];

    if(error != nil) {
        // Failed to write the file
        NSLog(@"Failed to write file: %@", [error description]);
    } else {
        UIAlertView *filenameAlert = [[UIAlertView alloc] initWithTitle:@"File saved" message:[NSString 
stringWithFormat:@"The file %@ has been saved", [requestedURL lastPathComponent]] delegate:self cancelButtonTitle:@"OK"
otherButtonTitles:nil];

        [filenameAlert show];
        [filenameAlert release];
    }

NSData NSDataWritingFileProtectionComplete example.
NSData *theData;
NSError *error;
[theData writeToURL:someURL
            options:NSDataWritingFileProtectionComplete
              error:&error];

End of NSData NSDataWritingFileProtectionComplete example article.