Search code examples
iosdatensurl

Why is the value of NSURLCreationDateKey nil on iOS 4.3 but not on iOS 5+?


I have a list of NSURL's for local stored files. I want to show the creation date, so I retrieve it as follows:

 for (NSURL * file in sortedFileList){
        NSDate *date = nil;
        NSError * error = nil;
        [file getResourceValue:&date forKey:NSURLCreationDateKey error:&error];
        if (error)
        {
            NSLog(@"error getting date: %@", error);
            continue;
        }
        if (!date)
        {
            NSLog(@"Empty date.... skipping file");
            continue;
        }
}

When I run this on iOS 5 simulator or iOS 6, it works ok and shows the dates. However, when I run it on the iOS 4.3 simulator it gives me nil for the dates and logs for all files the "Empty date...." message.

According to the docs NSURLCreationDateKey is available from iOS 4, so what's going on?


Solution

  • It turns out that the getResourceValue: forKey: method is only working on iOS 5+.

    Available in iOS 5.0 and later. (Symbol is present in iOS 4, but performs no operation.)

    I don't yet understand why the NSURLCreationDateKey is available for iOS 4, cause I can't find any method to get the date from the NSURl on another way.

    Solution for now:

    NSDate *date = [[[NSFileManager defaultManager] attributesOfItemAtPath:[url path] 
        error:nil] objectForKey:NSFileCreationDate];