让 iPhone 上显示学期周数(一) — — 环境搭建与测试
越狱开发所用的工具包有两种主流的选择 — — Theos 和 iOSOpenDev。前者较为简单,为命令行工具;后者则整合在 Xcode 里。这里我们将采用 Theos 作为工具进行开发,当然尽管如此,Xcode 仍然是必需的。
环境搭建
安装 Theos
首先在 profile 中配置环境变量 export THEOS=/opt/theos,然后从 GitHub 上安装 Theos,并设置目录的 owner。
sudo git clone --recursive https://github.com/theos/theos.git $THEOS
sudo chown $(id -u):$(id -g) /opt/theos
安装 ldid 和 dpkg
ldid 是越狱开发中的签名工具,dpkg 则是 Theos 用来将工程打包成 deb 文件的工具包。首先下载 Homebrew,然后执行如下命令。
brew install dpkg ldid
其他工具
开发中还需要用到一些其他的工具,一并在这里列出。
iOS
- OpenSSH:打开 iOS 设备的 SSH 通道,可在 Cydia 下载;
- Apple File Conduit “2”:让 Mac 可以访问 iOS 文件系统,可在 Cydia 下载;
- Cycript:一种脚本语言,可在 iOS 上执行,方便测试,可在 Cydia 下载。
macOS
- Hopper Disassembler:反汇编工具;
- iFunBox:访问 iOS 文件系统。
环境测试
创建工程
下面创建一个用于测试环境的示例工程,其功能为每次重启 SpringBoard 后弹窗显示 Hello World。
执行 /opt/theos/bin/nic.pl 后会启动创建向导,根据提示,选择项目类型为 iphone/tweak,工程名称为 helloword,其他选项参照填写即可。其中 MobileSubstrate Bundle filter 需填写插件作用对象的 bundle identifier,本例中就是 com.apple.springboard;List of applications to terminate upon installation 需填写插件安装完后需要重启的进程,本例中就是 SpringBoard。
~ /opt/theos/bin/nic.pl
NIC 2.0 - New Instance Creator
------------------------------
[1.] iphone/application
[2.] iphone/flipswitch_switch
[3.] iphone/library
[4.] iphone/preference_bundle
[5.] iphone/tool
[6.] iphone/tweak
Choose a Template (required): 6
Project Name (required): helloworld
Package Name [com.yourcompany.helloworld]: com.wangjinli.helloworld
Author/Maintainer Name [Li]: Li
[iphone/tweak] MobileSubstrate Bundle filter [com.apple.springboard]: com.apple.springboard
[iphone/tweak] List of applications to terminate upon installation (space-separated, '-' for none) [SpringBoard]: SpringBoard
Instantiating iphone/tweak in helloworld/...
Done.
编辑 Makefile
在模板生成的 Makefile 中添加处理器架构、调试设备 IP、编译器和 SDK 版本,并导入 UIKit,最终内容如下。
THEOS_DEVICE_IP = IOSIP
ARCHS = armv7 arm64
TARGET = iphone:clang:latest:8.0
include theos/makefiles/common.mk
TWEAK_NAME = helloworld
helloworld_FILES = Tweak.xm
helloworld_FRAMEWORKS = UIKit
include $(THEOS_MAKE_PATH)/tweak.mk
after-install::
install.exec "killall -9 SpringBoard"
编辑 Tweak.xm
这是 Tweak 的主文件,使用 Logos 语法。在这里我们 hook SpringBoard 这个类,重写 applicationDidFinishLaunching 方法,在执行该方法原本内容之后,弹出一个 UIAlertView。最终内容如下。
#import <UIKit/UIKit.h>
%hook SpringBoard
- (void)applicationDidFinishLaunching: (id)application {
%orig;
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hello World." message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
%end
编译安装
在工程目录执行 make 即可编译,执行 make package 可打包为 deb 文件。在 Makefile 中填写了 THEOS_DEVICE_IP 字段的情况下可使用 make package install 命令完成编译、打包和安装。
~/helloworld make package install
Making all for tweak helloworld...
make[2]: Nothing to be done for `internal-library-compile'.
Making stage for tweak helloworld...
dm.pl: building package `com.wangjinli.helloworld:iphoneos-arm' in `./com.wangjinli.helloworld_0.0.1-1_iphoneos-arm.deb'
Unloading MobileCydia...
root@localhost's password:
Installing...
root@localhost's password:
tar: ./control: time stamp 2016-08-06 13:22:46 is 28.884294 s in the future
tar: .: time stamp 2016-08-06 13:22:46 is 28.880513 s in the future
Selecting previously deselected package com.wangjinli.helloworld.
(Reading database ... 3955 files and directories currently installed.)
Unpacking com.wangjinli.helloworld (from /tmp/_theos_install.deb) ...
Setting up com.wangjinli.helloworld (0.0.1-1) ...
install.exec "killall -9 SpringBoard"
root@localhost's password:
期间需要在上传、安装和注销的时候输入三次设备的 root 密码,比较繁琐,可将 Mac 的公钥添加至设备的 known_hosts 中以省去这一步骤。
设备注销之后就可以看到插件已经生效。

至此环境搭建与测试部分完成,下一步就要着手 WeekCount 的开发了。