iOS actually makes this really easy for you.
I don't know what else you might want your daemon to do, but if you only wanted it to stay alive continuously to watch for new picture files, then you have another alternative.
You can configure a Launch Daemon to be started, only when new file system events are detected. See the Apple docs on (OS X) Launch Daemons here
Your launch daemon executable can then just be a simple main()
program. It will be started by the system when a new picture file is written, and you can then use NSFileManager
or ALAssetLibrary
to check the directory for the newest file(s). You might want to save a preference that indicates when the last time the daemon was run, to make sure you keep track of all new files.
int main(int argc, char *argv[]) {
// if we're here, we know there's a new picture, so use
// NSFileManager to check for photos
// or, see something like http://stackoverflow.com/q/9730973/119114 ...
// and then we exit the process and let launchd start us
// again when there's more pictures
return 0;
}
The key here is to use a /System/Library/LaunchDaemons/com.example.MyApp.plist
file like this:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.example.MyApp</string>
<key>ProgramArguments</key>
<array>
<string>/Applications/MyApp.app/MyDaemonExecutable</string>
<string>optional_argument_one</string> <!-- passed to main() as argv[] -->
<string>optional_argument_two</string> <!-- passed to main() as argv[] -->
</array>
<key>WatchPaths</key>
<array>
<string>/private/var/mobile/Media/DCIM/100APPLE</string>
<string>/private/var/mobile/Media/DCIM/101APPLE</string>
</array>
</dict>
</plist>