閱讀之前建議先看下 Run a daemon (as root) on iOS辅髓,因?yàn)楹芏嗯渲枚己蚷OS10里面是一樣的;
1.環(huán)境:iOS 11.0.1
Xcode9.4
安裝最新版MonkeyDev(ps:已經(jīng)安裝的就更新,慶總有更新:fix invalid provision)
2.Xcode新建項(xiàng)目细溅,選擇:
3.建好的工程如下:
其中monkeydev.entitlements文件就是MD更新后新增的卖陵;我把main.c改為了main.mm;將里面代碼改為:
用來(lái)驗(yàn)證守護(hù)進(jìn)程是否正確運(yùn)行;
4.進(jìn)入工程Finder中侧啼,在package文件夾下面新建兩個(gè)文件夾牛柒,名字及關(guān)系為:/Library/LaunchDaemons/,在LaunchDaemons文件夾中創(chuàng)建plist文件痊乾,名字為com.ztwl.demo.plist;保證和contol里面的package名字一致皮壁;
如圖:
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>com.ztwl.demo</string>
<key>ProgramArguments</key>
<array>
<string>/usr/bin/demo</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.同樣打開Finder,進(jìn)入DEBIAN文件夾下哪审,添加兩個(gè)文件postinst蛾魄、prerm;
prerm 代碼為:
#!/bin/sh
chown -R root:wheel /usr/bin/demo
chown -R root:wheel /Library/LaunchDaemons/com.ztwl.demo.plist
/bin/launchctl unload /Library/LaunchDaemons/com.ztwl.demo.plist
/bin/launchctl load /Library/LaunchDaemons/com.ztwl.demo.plist
prerm 代碼為:
#!/bin/sh
/bin/launchctl unload /Library/LaunchDaemons/com.ztwl.demo.plist
其中 postinst文件:是插件安裝后執(zhí)行的腳本湿滓;
prerm文件:軟件卸載前需要執(zhí)行的腳本滴须。
6.工程配置好端口號(hào)等參數(shù),編譯插件叽奥;
7.驗(yàn)證:我用console查看扔水,效果如下
補(bǔ)充:參考iOS10 iphoneDaemonTool
至此iOS11上面守護(hù)進(jìn)程運(yùn)行完畢。
記錄了實(shí)現(xiàn)過(guò)程而线,感謝狗神及慶總的幫助铭污。有不對(duì)的地方請(qǐng)指正。