OGeek|极客世界-中国程序员成长平台

标题: ios - 文件系统事件通知(越狱设备) [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 10:00
标题: ios - 文件系统事件通知(越狱设备)

当有新的文件系统事件时,我需要能够接收通知。 例如,添加新图片时。我需要能够接收整个文件系统的这些通知,而不仅仅是我的应用所在的沙箱。这是针对越狱的设备吗?

有人知道我应该使用哪个私有(private) API 吗?

我的应用程序是一个守护程序应用程序,在后台运行,它必须能够接收这些事件。



Best Answer-推荐答案


iOS 实际上让这对你来说真的很容易。

我不知道您可能希望您的守护进程做什么其他,但如果您只想让它持续保持事件状态以监视新的图片文件,那么您还有另一种选择。

您可以配置启动守护程序以仅在检测到新文件系统事件时启动。 See the Apple docs on (OS X) Launch Daemons here

您的启动守护程序可执行文件可以只是一个简单的 main() 程序。当新的图片文件写入时,系统会启动它,然后您可以使用 NSFileManagerALAssetLibrary 来检查目录中是否有最新的文件。您可能希望保存一个指示上次运行守护程序时间的首选项,以确保跟踪所有新文件。

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;
}

这里的关键是使用 /System/Library/LaunchDaemons/com.example.MyApp.plist 文件,如下所示:

<?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>rogramArguments</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> 

关于ios - 文件系统事件通知(越狱设备),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15728248/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4