iphone开发后台常驻进程

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值