安裝Homebrew
- 利用brew安裝ldid
brew install ldid
修改環(huán)境變量
- 編輯用戶的配置文件
$ vim ~/.bash_profile
- 在.bash_profie文件后面加入以下2行
export THEOS=~/theos
export PATH=$THEOS/bin:$PATH
- 讓.bash_profiel配置的環(huán)境變量立即生效
$ 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的目標類
]
重寫方法實現(xiàn)
%end
%hook WCLoginView
- (id)initWithFrame:(struct CGRect)arg1 {
return nil;
}
%end
類名可以通過Reveal連上手機查看
方法名可以通過class-dump導出.h文件查看
%new :添加一個新的方法
%orgi :函數(shù)原來的代碼邏輯
%ctor
%log
6. 資源文件(圖片)
- 在項目目錄下新建layout文件夾,對應著手機的根目錄/
7. 編譯打包安裝
- 編譯Tweak代碼為動態(tài)庫(*.dylib)
$ make
- 將dylib打包為deb文件
$ make package
- 將deb文件傳送到手機上胸囱,通過Cydia安裝deb(默認會重啟SpingBoard)
插件會安裝在/Library/MobileSubstrate/DynamicLibraries文件夾中
$ make install
小技巧:把多個命令合并,然后配置成.sh
腳本
$ make clean && make && make package && make install //從前到后按順序執(zhí)行
8. 原理
1、theos的tweak并不會對APP原來的可執(zhí)行文件進行修改蚁趁,僅僅是修改了內(nèi)存中的代碼邏輯据途。
- 當打開APP時,Cydia Substrate(Cydia 的插件)會讓APP自動去加載對應的dylib扛邑。
- 修改APP內(nèi)存中的代碼邏輯怜浅,去執(zhí)行dylib的函數(shù)代碼。
logify.pl 注意點
- 終端打開TestController.h所在目錄蔬崩,使用logify.pl 生成TestController.xm文件
$ logify.pl TestController.h > TestController.xm
- 但是生成的xm文件經(jīng)常在make的時候編譯不過恶座,需要手動處理代碼
- 刪掉__weak
- 刪掉inout
- 協(xié)議報錯:
1)刪掉協(xié)議
2)聲明協(xié)議@protocol XXTestDelegate
- 刪掉- (void).cxx_destruct { %log; %orig; }
- 刪掉HBLogDebug(@" = 0x%x", (unsigned int)r); 或者 HBLogDebug(@" = 0x%@", r);
- 類名報錯:
1)替換類名為void:比如將TestModel *
替換為void *
2)聲明類信息:@class TestModel