Iosdaemon程序的编写

1.theos创建工程

 nic.pl 
NIC 2.0 - New Instance Creator
------------------------------
  [1.] iphone/activator_event
  [2.] iphone/application_modern
  [3.] iphone/application_swift
  [4.] iphone/flipswitch_switch
  [5.] iphone/framework
  [6.] iphone/library
  [7.] iphone/preference_bundle_modern
  [8.] iphone/tool
  [9.] iphone/tool_swift
  [10.] iphone/tweak
  [11.] iphone/xpc_service
Choose a Template (required): 8
Project Name (required): daemontest
Package Name [com.yourcompany.daemontest]: com.abc.daemontest
Author/Maintainer Name [bichonfrise]: abc
Instantiating iphone/tool in daemontest/...
Done.
cd daemontest/

2.修改main.m

vim main.m

#include <stdio.h>
#include <UIKit/UIKit.h>

int main(int argc, char *argv[], char *envp[]) {
	int i = 0;
	while(1){
		NSLog(@"DaemonTest %d", i);
		i++;
		sleep(1);
	}
	return 0;
}

  

3. 写plist

mkdir -p layout/Library/LaunchDaemons
cd layout/Library/LaunchDaemons/
vi com.abc.daemontest.plist
<!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>com.abc.daemontest</string>
        <key>ProgramArguments</key>
        <array>
                <string>/usr/bin/daemontest</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
        <key>UserName</key>
        <string>root</string>
</dict>
</plist>

4. 打包安装

make package
> Making all for tool daemontest…
==> Compiling main.m (armv7)…
==> Linking tool daemontest (armv7)…
ld: warning: OS version (6.0.0) too small, changing to 7.0.0
==> Generating debug symbols for daemontest…
==> Compiling main.m (arm64)…
==> Linking tool daemontest (arm64)…
ld: warning: OS version (6.0.0) too small, changing to 7.0.0
==> Generating debug symbols for daemontest…
==> Compiling main.m (arm64e)…
==> Linking tool daemontest (arm64e)…
ld: warning: OS version (6.0.0) too small, changing to 7.0.0
==> Generating debug symbols for daemontest…
==> Merging tool daemontest…
==> Signing daemontest…
> Making stage for tool daemontest…
dm.pl: building package `com.abc.daemontest:iphoneos-arm' in `./packages/com.abc.daemontest_0.0.1-1+debug_iphoneos-arm.deb'
make install
==> Installing…
Selecting previously unselected package com.abc.daemontest.
(Reading database ... 8834 files and directories currently installed.)
Preparing to unpack /tmp/_theos_install.deb ...
Unpacking com.abc.daemontest (0.0.1-1+debug) ...
Setting up com.abc.daemontest (0.0.1-1+debug) ...

5. 跑起来

ssh root@localhost -p2222
launchctl load /Library/LaunchDaemons/com.abc.daemontest.plist

6. 检查

7. 停掉

launchctl unload /Library/LaunchDaemons/com.abc.daemontest.plist

交流vx:Nicholas_mc

用xpc_service吧。