2

How to detect whether a file is modified in iOS?

I don't mean the real-time monitoring, because while the application is gone, it won't detect anymore.

I've checked the attributes of the file, and couldn't find the satisfied one.(Did I miss something?)

There maybe some check-sum generating solutions, but they will require much time. (Is there any cheap check-sum generating algorithm?)

AstroCB
  • 12,337
  • 20
  • 57
  • 73
Yun
  • 5,233
  • 4
  • 21
  • 38

2 Answers2

2

You can use stat (reference), to get the modification time of the file. You could then compare it against your stored value:

struct stat sb;
if (stat("/path/to/file", &sb) == 0)
{
    ... compare sb.st_mtimespec with stored value

}
else
{
    ... report error
}

This question is pretty close to yours.

Community
  • 1
  • 1
trojanfoe
  • 120,358
  • 21
  • 212
  • 242
1

Which attributes did you check? With fstat(), you should be able to compare the st_mtime attribute of a file with a previously stored value.

Alex Reynolds
  • 95,983
  • 54
  • 240
  • 345
  • Is it the same as the NSFileModificationDate? – Yun Feb 29 '12 at 07:55
  • It could be the same, but apparently `stat` works much faster. See the following SO answer that demonstrates the use of `stat` to get the modification date: http://stackoverflow.com/a/1999553/19410 – Alex Reynolds Feb 29 '12 at 08:06
  • It's not made clear, but that sample code looks like an Objective C category that extends `NSDate` (if you're interested in that detail). – Alex Reynolds Feb 29 '12 at 08:08
  • Ah, now I know why that was bothering me. I think if you try to send an app to Apple for approval, which contains a category that extends a class in an Apple API (like `NSDate`), then said app will be rejected. So I would use that code to learn `stat`, and then modify it into a method in a "local", non-Apple helper class. – Alex Reynolds Feb 29 '12 at 08:31