开发者

Restore file with data cocoa?

How can I restore a file with certain data. If a user enters a file in which he wants the data to be restored开发者_Python百科 with, and that file is not there, it should be created with that data. This is all part of an encryption tool. So say I, the user, selects a file, and encrypts it. So I have my encrypted data. But now I need to move this encrypted data to a file, so that the user can take the file that contains the encrypted data, to decrypt, so that he gets his original file provided he entered the correct key.

Hopefully that was clear enough. All help is greatly appreciated.


Use NSFileManager to get (and possiby create) the file path for the data, then writeToFile: methods. Something like this:

- (void) saveCriteriaToFile: (NSArray *) criteria
{       
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *supportDir = [[paths objectAtIndex:0] stringByAppendingPathComponent: @"StokerX"];

NSString *saveFilePath = [supportDir stringByAppendingPathComponent: kSavedNotificationsFile];

NSFileManager *fileManager = [NSFileManager defaultManager];

if ([fileManager fileExistsAtPath: saveFilePath] == NO)
{
    [fileManager createDirectoryAtPath: supportDir withIntermediateDirectories:YES attributes:nil error:nil];
}

[criteria writeToFile:saveFilePath atomically:YES];
}

This one is writing an NSArray object to a pilist, but you do it almost the same with an NSData object.


I fixed my problem by using NSHomeDirectory instead of hardcoding the path. Thanks though for the help.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜