Content modification date of an file in Cocoa - ios

How can I find out when a file was last time modified in Cocoa?
I tried using NSFileModificationDate attribute of an NSFile but the modification date gets update when you read a file. I just want to know when it was last time changed like in the Mac OS X Finder.

You can try next code:
NSDate *fileModificationDate = nil;
[fileUrl getResourceValue:&fileModificationDate forKey:NSURLContentModificationDateKey error:nil];
NSLog(#"modification date %#", fileModificationDate)
You can see the resource keys in NSURL.h - NSURLContentModificationDateKey and other.
Hope it will help.

Related

How to select a sub-string from an NSString using a key word?

I had written a code snippet which writes the contents of a textfield to a file. Unfortunately my code, depending on the OS, writes the file path differently
In Yosemite, The path is
file:///var/folders/qg/....../myfile.txt
While in mountain lion the path is
file://localhost/var/folders/yx....../myfile.txt
I have an API which takes the file path as /var/folder/xx/...../myfile.txt
I was wondering if there is a way to make a substring from /var/.. till the end of the path.
You could do something along the following:
// Lets say pathString is an NSString of the path
NSRange varRange = [pathString rangeOfString:#"/var/"];
NSString *correctPath = [pathString substringFromIndex:varRange.location];
In the above example, you use NSString's instance method rangeOfString: to receive the range of the wanted substring, which in this case is /var/, and store it into a range variable.
Then you create a new NSString variable, using the original pathString, with the use of the instance variable substringFromIndex, which returns a new substring, which start at the index you choose, and ends at the end of the string (which you provide the range location we've received, to identify where /var/ begins).
Good luck mate.
If file path is store in NSString *yourPathString, then
NSString *resultString= [yourPathString substringFromIndex:[yourPathString rangeOfString:#"/var"].location]];
NSLog(#"Final result : %#", resultString);
Try this code, hope it helps.
Depending on how you're calling your resource, how it needs to be saved, and what that API is looking for, you'll want to use NSURL methods, not strings. Specifically, it sounds like you'll be most interested in using absolutePath, relativePath, resourceIdentifierand pathComponents
I would recommend reading the URL Loading System guide by Apple or NSHipster's article for a more human readable answer.

Objective Zip archives aren't opened by ArchiveUtility.app

I'm using ObjectiveZip library (which is a wrapper for MiniZip) in my iOS app. My app sends .zip archives to server, where they are processed manually by moderator. Here's my code for creating an archive:
NSString * zipfile = [Result zipfilePathWithResult:self];
ZipFile * zf = [[ZipFile alloc] initWithFileName:zipfile mode:ZipFileModeCreate];
ZipWriteStream * zws = [zf writeFileInZipWithName:#"report.xml" compressionLevel:ZipCompressionLevelNone];
[zws writeData:[xml dataUsingEncoding:NSUTF8StringEncoding]];
[zws finishedWriting];
for (NSString * name in mediaFiles)
{
ZipWriteStream * zws = [zf writeFileInZipWithName:name compressionLevel:ZipCompressionLevelNone];
[zws writeData:[files objectForKey:name]];
[zws finishedWriting];
}
[zf close];
Unfortunately, these archives aren't correctly processed by default OS X ArchiveUtility: it always unarchives my files into zip.cpgz, regardless of the actual content of archive (usually it's a .xml file and a few .jpg files).
However, some other applications on OS X and also on Windows are able to open my archives correctly, so the archive isn't actually broken.
Is there a way to make ObjectiveZip work with ArchiveUtility? Or maybe you can suggest any other objective-c library for creating .zip files which can do it. Thanks in advance!
Edit zip.c changing the parameters passed into each calls to zipOpenNewFileInZip3_64. The last parameter being passed into each method is a 1. Change it to a 0 and it will work
Changing the last parameter to zipOpenNewFileInZipX_64 function is one way to fix this but changing this parameter to 0 means setting zip64, i.e large file zipping capability, to false.
I was able to fix this by setting second parameter of zip64local_putValue call to (uLong)20(which is currently 45 in one case) inside the function Write_LocalFileHeader regardless of value of zi->ci.zip64.

Comparing creation/modification date of NSData objects

At first I have a plist file in app bundle. At some point the file can get updated (downloaded into Documents Folder). Whats the best way to know which file is newer? The mainbundle plist could get updated with new App Version and then the downloaded one would be the oldest.
I do have a timestamp inside the plist but I actually don't want to load each of them into memory to be able to compare the dates, as each takes 1-2 seconds.
Is there some kind of creation date that I could compare?
How about this:
NSError *error = nil;
NSDictionary* dict = [NSFileManager attributesOfItemAtPath:path error:&error];
NSDate* date = [dict fileModificationDate];
You can use -[NSFileManager attributesOfItemAtPath:error:] to get the filesystem-level attributes of each plist, then compare values for modification dates (using the NSFileModificationDate string constant).

Invalidating QLPreviewController "cache"

QLPreviewController seems to cache file contents based on the local file's URL. In my application, the file contents can be updated remotely and would cause the new contents to be downloaded.
If I view a file in QLPreviewController, update it remotely, then re-preview it, the file does not show up as updated.
The file is definitely updated on disk, and other controls show the correct updated file.
The workaround I'm using right now is to basically move a file when it's previewed to a unique filename (using timestamp), which will of course not be in the QLPreviewController's cache. However, this has other repercussions, for example, if the app is killed or it crashes (god forbid), I won't know "where" to find the downloaded file.
I'm looking for less invasive hacks, or solutions to making QLPreviewController refresh its cache. The APIs don't seem to expose anything, so don't be afraid to submit a hack if it's less gross than the one I've presented above (not including copying/moving the file to a guaranteed unique URL, which I am already utilizing).
Just ran into this issue myself. I solved it by recreating the QLPreviewController each time I reload an item with the same name as the currently viewed item. Creating a new QLPreviewController clears the cache.
I know this is an old question but someone might have the same problem and find this answer helpful.
You should use refreshCurrentPreviewItem after downloading complete
I had the same problem. Opening a locally generated CSV file.
I have my _previewController* setup as a #property of my controller. Then what i did:
- (void)viewDidLoad
{
[super viewDidLoad];
self.previewController = [[QLPreviewController alloc] init];
_previewController.delegate=self;
_previewController.dataSource=self;
}
- (void)previewCSV
{
[_previewController reloadData]; // this triggers a reload
[self presentModalViewController:_previewController animated:YES];
}
IN other solution that comes to mind (not tested).
Depending on your URL, you could add something like http://url?time=123456 to your URL. Like this you change the URL but without side effect. The time (or any other parameter) you can change on each request.
It's the ugliest bug in iOS. Cache management in iOS 5 and beyond. I think is the same reason that makes iCloud buggy, Share-at-Home crashing and so on. Bad cache managements and so worst synchronization systems.
Well, my solution for this was to store the download file in a folder and use the current date to name the folder. It is equivalent to #Rogier's solution, but this works always. You get a name for the folder, for example, with [[NSDate date] description]. Instead of saving the file replacing the old one, you delete previous file, delete previous folder and save new file in a new folder. It's working fine for me.
Just remove all files from tmp directory like this:
- (void)clearCache
{
NSString *tempPath = NSTemporaryDirectory();
NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:tempPath error:nil];
NSFileManager *fileManager = [NSFileManager defaultManager];
for (int i = 0; i < [dirContents count]; i++) {
NSLog(#"Directory Count: %i", [dirContents count]);
NSString *contentsOnly = [NSString stringWithFormat:#"%#%#", tempPath, [dirContents objectAtIndex:i]];
[fileManager removeItemAtPath:contentsOnly error:nil];
}
}

DatePicker not returning selected date iOS

I am trying to get the selected date from a DatePicker and all I get is the current system date. Any ideas why?
I am doing it through the simulator as I do not have a device. Here is the code I use to get the date.
[self scheduleLocalNotification:timerPicker.date];
I have also used this to see if it works:
NSDate *d = [timerPicker date];
[self scheduleLocalNotification:d];
After several reboots of XCode and no code changes it magically started working.

Resources