nsfilemanager createFileAtPath results in NSFileCreationDate being null - ios6

I am creating an image file from a json object and the file creation is successful and the image is indeed created and can be displayed. I want a process that will check if the file is still in cache or a tmp directory before downloading the image. If the image is indeed there then I want to check the creation date and modification date attributes of the file to see if I need to download a new version of the image to keep it up to date. This is where the problem comes in.
The creation and modification date attributes are null. I've even attempted to manually set the attributes and it didn't change anything.
Here's the code I use to create the file:
NSString *fileName = [NSHomeDirectory() stringByAppendingPathComponent: [NSString stringWithFormat:#"tmp/%#",theFileName]];
BOOL filecreationSuccess =[fileManager createFileAtPath:fileName contents:myData attributes:nil];
if(filecreationSuccess == NO){
NSLog(#"Failed to create the file");
}
I have even tried adding the Code and the attributes on the file are still null:
NSDate *now = [NSDate date];
NSDictionary *createDateAttr = [NSDictionary dictionaryWithObjectsAndKeys: now, NSFileCreationDate,nil];
NSDictionary *modDateAttr = [NSDictionary dictionaryWithObjectsAndKeys: now, NSFileModificationDate, nil];
[fileManager setAttributes:modDateAttr ofItemAtPath:fileName error:&err];
[fileManager setAttributes:createDateAttr ofItemAtPath:fileName error:&err];
I am unsure of what to do next. Any help would be greatly appreciated, Thank you.

I have found my problem. It wasn't that the modification and creation dates weren't being set, but that I was not getting the correct set of properties for the file. It ended up being that the code I was using was getting the properties for the path that the file was in, not the file itself.

Related

UIFIleSharing populating UIPickerView

I'm new to the site and probably even newer to app development, however i'm learning slowly.. lol
So i hit my first big snag, I've researched all over on the web and can't find what I'm looking for, so here we go.
I'm creating an app that calls upon some user inputted files that can and do change on occasion. I've added the UIFileSharing option for this so users can upload files via iTunes.
There are 3 different types of files that will need to be used, a .opt, a .pkg, and a .txt.
Is there some way i can take the files from the directory, read them, and based off the file extension pull them and use them in a UIPickerView wheel? I'm really new at this so please forgive me when i ask you to be specific.
My assumption is to do this in a few steps, first read the files and sort them and place them in an array based on the extension, then use said array to populate the picker, and also to get the count for number of rows etc..
I guess the second two parts are pretty simple to figure out, just setting up a picker to use an array, i just need to know if its possible to build that array based on the user loaded files.
thanks in advance,
Chuck
You'll need to use the UIDocumentInteractionController class.
Simple Example
UIDocumentInteractionController * controller;
NSURL *fileURL=[NSURL fileURLWithPath:[self getFilePath]]
controller = [ UIDocumentInteractionController interactionControllerWithURL: fileURL ];
// How to get File From the Document Directory
-(NSString *)getFilePath{
NSString *documentPath= [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
return [documentPath stringByAppendingPathComponent:#"Your file name"]
}
I solved it guys, thanks for all the help.. Code is below, for anyone interested. Don't forget to define your arrays!
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *bundleDirectory = [fileManager contentsOfDirectoryAtPath:documentsPath error:nil];
NSPredicate *Option = [NSPredicate predicateWithFormat:#"self ENDSWITH '.opt'"];
_OptionArray = [bundleDirectory filteredArrayUsingPredicate:Option];
_OptionPickerData = [_OptionArray copy];

Update existing plist on iOS5 simulator

I am trying to update an existing plist on iOS5 simulator but it fails miserably.
It finds the plist and retrieves existing information and updates the array but it does not write to file.
Any one would have any ideas?
NSString *path = [[NSBundle mainBundle] pathForResource:#"content_iPhone" ofType:#"plist"];
NSMutableArray *plist = [[NSMutableArray arrayWithContentsOfFile:path] mutableCopy];
// For testing purpose, get an object from the Array and add the object to array
newObject = [plist objectAtIndex:1];
[plist addObject:newObject];
[plist writeToFile:path atomically:YES];
You can't write to a file inside your app bundle, you can only read from it.
You could write the file to the user's Documents directory instead: look at
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)
This returns an array, the first object of which will be the path to the user's Documents directory for your app. You could copy the file there the first time your app is run, and update it at that location in the future.

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 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.

Strange bug when renaming a file with NSFileManager

I used to use the following to rename aplist file with various user inputted values (BOOL,Strings mostly):
[manager moveItemAtPath:oldPath toPath:newPath error:&error];
And for some reason in iOS4+ this glitches out, so I Attempt the following:
[1] [manager copyItemAtPath:oldPath toPath:newPath error:&error];
[2] [manager removeItemAtPath:oldPath error:&error];
Now, [1] copies the plist and ALL of the data correctly. (I've verified when commenting out [2]. But for some reason, when I attempt [1] & [2], the file /appears/ renamed, however, there are only certain fields missing, for example I FName and LName stay intact, whereas Phone1,Phone2,Email1,Email2 and Website are removed from the new file.
For the life of me, I just cannot figure out why only some of the data remains when I attempt a removeFileAtPath for the OLD file AFTER the NEW FILE is created?
Any help would be MUCH APPRECIATED!
I just simply want to rename a plist file, while keeping ALL of the data in tact.
NSString *newPath = [[oldPath stringByDeletingLastPathComponent] stringByAppendingPathComponent:newFilename];
[[NSFileManager defaultManager] movePath:oldPath toPath:newPath handler:nil];

Writing to a .plist file

I have a .plist with 2 key values in it. It is of type Dictionary. I am trying to write value to one of the key values. What's wrong with the code below? I also tried using type "Array". That option also does not work. How can I get it to work using Dictionary & also Array? Anyone has working code example? Thanks. I would appreciate any help.
NSString *filePath = #"myprefs.plist";
NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
[plistDict setValue:#"test#test.com" forKey:#"Email"];
[plistDict writeToFile:filePath atomically: YES];
You did not initialize the path properly.
If "myprefs.plist" is inside the resources folder of your project then initialize like this:
NSString *filePath = [[NSBundle mainBundle]pathForResource:#"myprefs" ofType:#"plist"];
If its somewhere else in your computer, specify the whole path

Resources