iphone开发后台常驻进程
1.程序代码
#include <stdio.h>
#include <UIKit/UIKit.h>
int main() {
int i = 0;
while(1) {
NSLog(@"Daemon test %d", i);
i++;
sleep(5);
}
return 0;
}
2.编译
clang -arch armv7 -isysroot $(xcrun --sdk iphoneos -show-sdk-path) -framework Foundation -o daemonTest main.m
3.签名
codesign -s - --entitlements ent.plist -f daemonTest
ent.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>com.apple.springboard.debugapplications</key>
<true/>
<key>run-unsigned-code</key>
<true/>
<key>get-task-allow</key>
<true/>
<key>task_for_pid-allow</key>
<true/>
</dict>
</plist>
4.常驻配置文件
net.daemonTest.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>KeepAlive</key>
<true/>
<key>Label</key>
<string>net.daemonTest</string>
<key>ProgramArguments</key>
<array>
<string>/usr/bin/daemonTest</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>SessionCreate</key>
<true/>
<key>StandardErrorPath</key>
<string>/dev/null</string>
<key>inetdCompatibility</key>
<dict>
<key>Wait</key>
<false/>
</dict>
</dict>
</plist>
5.上传文件到iphone
daemonTest放到/usr/bin
net.daemonTest.plist放到/Library/LaunchDaemons
6.设置权限
:~ root# chown root:wheel /usr/bin/daemonTest
:~ root# chmod 755 /usr/bin/daemonTest
:~ root# chown root:wheel /Library/LaunchDaemons/net.daemonTest.plist
7.启动
launchctl load /Library/LaunchDaemons/net.daemonTest.plist
停止就是unload