安装Homebrew
- 利用brew安装ldid
brew install ldid
修改环境变量
- 编辑用户的配置文件
$ vim ~/.bash_profile
- 在.bash_profie文件后面加入以下2行
export THEOS=~/theos
export PATH=$THEOS/bin:$PATH
- 让.bash_profiel配置的环境变量立即生效
$ source ~/.bash_profile
下载theos
$ git clone --recursive https://github.com/theos/theos.git $THEOS
新建tweak项目
1. cd到一个存放代码的文件夹,输入nic.pl
$ cd ~/Desktop
$ nic.pl
2. 选择iphone/tweak所对应的编号,下面是10
$ ~/Desktop 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): 10
3. 填写项目信息
- Project Name (required):项目名称
- Package Name:项目ID(随便写com.test.name)
- Author/Maintainer Name:作者
- [iphone/tweak] MobileSubstrate Bundle filter:目标APP的Bundle Identifier,
- [iphone/tweak] List of applications to terminate upon installation:直接敲回车
Project Name (required): my_tweak
Package Name [com.yourcompany.ting_tweak]: com.tweak.wechat
Author/Maintainer Name [Mac]:
[iphone/tweak] MobileSubstrate Bundle filter [com.apple.springboard]:com.tencent.wechat
[iphone/tweak] List of applications to terminate upon installation (space-separated, '-' for none)[SpringBoard]:
Instantiating iphone/tweak in ting_tweak/...
Done.
4. 编辑Makefile
THEOS_DEVICE_IP
THEOS_DEVICE_PORT
通过哪个IP和端口号访问手机
export THEOS_DEVICE_IP=127.0.0.1
export THEOS_DEVICE_PORT=10010
include $(THEOS)/makefiles/common.mk
TWEAK_NAME = ting_tweak
ting_tweak_FILES = Tweak.xm
include $(THEOS_MAKE_PATH)/tweak.mk
after-install::
install.exec "killall -9 SpringBoard"
也可以把这两个变量配置到.bash_profile中
$ vim ~/.bash_profile
export THEOS_DEVICE_IP=127.0.0.1
export THEOS_DEVICE_PORT=10010
$ source ~/.bash_profile
5. 编写代码(Tweak.xm文件)
%hook [需要hook的目标类
]
重写方法实现
%end
%hook WCLoginView
- (id)initWithFrame:(struct CGRect)arg1 {
return nil;
}
%end
类名可以通过Reveal连上手机查看
方法名可以通过class-dump导出.h文件查看
%new :添加一个新的方法
%orgi :函数原来的代码逻辑
%ctor
%log
6. 资源文件(图片)
- 在项目目录下新建layout文件夹,对应着手机的根目录/
7. 编译打包安装
- 编译Tweak代码为动态库(*.dylib)
$ make
- 将dylib打包为deb文件
$ make package
- 将deb文件传送到手机上,通过Cydia安装deb(默认会重启SpingBoard)
插件会安装在/Library/MobileSubstrate/DynamicLibraries文件夹中
$ make install
小技巧:把多个命令合并,然后配置成.sh
脚本
$ make clean && make && make package && make install //从前到后按顺序执行
8. 原理
1、theos的tweak并不会对APP原来的可执行文件进行修改,仅仅是修改了内存中的代码逻辑。
- 当打开APP时,Cydia Substrate(Cydia 的插件)会让APP自动去加载对应的dylib。
- 修改APP内存中的代码逻辑,去执行dylib的函数代码。
logify.pl 注意点
- 终端打开TestController.h所在目录,使用logify.pl 生成TestController.xm文件
$ logify.pl TestController.h > TestController.xm
- 但是生成的xm文件经常在make的时候编译不过,需要手动处理代码
- 删掉__weak
- 删掉inout
- 协议报错:
1)删掉协议
2)声明协议@protocol XXTestDelegate
- 删掉- (void).cxx_destruct { %log; %orig; }
- 删掉HBLogDebug(@" = 0x%x", (unsigned int)r); 或者 HBLogDebug(@" = 0x%@", r);
- 类名报错:
1)替换类名为void:比如将TestModel *
替换为void *
2)声明类信息:@class TestModel
网友评论