前言

上一篇 部署 Theos 越獄開發環境 中,已完成越獄開發環境的部署。

接下來,將新增 Tweak 專案並著手編寫第一個越獄套件。

此範例要實現的功能十分基礎,即是於 Respring 後,彈出一個警告窗框,顯示自定義的文字。


關於越獄套件

在 Cydia 中,預設安裝了這個套件:Cydia Substrate。

Cydia Substrate 是一個框架,能讓程式在本身不變的情況下,被開發者自定的函式覆蓋掉原本的功能,是開發越獄套件的基石。

iOS 負責處理使用者介面的程式為 SpringBoard.app。

前言提及的 Respring 是 Restart SpringBoard 的縮寫,作用是重新載入主畫面。

多數擴充套件會修改 SpringBoard 的相關功能,本範例也是如此,所以 Respring 後才能讓越獄套件實現作用。


實作過程

新增專案

  1. cd到要存放專案的目錄,於終端機下指令,開啟 Theos 的 NIC 介面:

    $THEOS/bin/nic.pl
    
  2. 輸入 iphone/tweak 的模板代號,本例為 10:

    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. 輸入專案相關資訊,或直接Enter套用 內的預設值:

    Project Name (required): hello(專案名稱)
    Package Name [com.yourcompany.hello]: tw.yuripe-dev.hello(唯一識別名稱)
    Author/Maintainer Name [yuripeyamashita]:(作者/維護者名稱)
    [iphone/tweak] MobileSubstrate Bundle filter [com.apple.springboard]:(欲植入的可執行檔 Bundle ID)
    [iphone/tweak] List of applications to terminate upon installation (space-separated, '-' for none) [SpringBoard]:(擴充套件安裝完成後欲終止的程序)
    
  4. 新增專案完成:

    Instantiating iphone/tweak in hello/...
    Done.
    

    此時,於 hello 專案目錄下有數個檔案,以下分別介紹檔案用途。


.plist

此檔案用於紀錄擴充套件的作用範圍。

{ Filter = { Bundles = ( "com.apple.springboard" ); }; }

若使用 Xcode 打開該文件,最外層是個字典(Dictionary),其中有個名為Filter的鍵值。

Filter下是一系列的陣列(Array),可分為三類:

  • Bundles:指定 Bundle 為作用對象,例:com.apple.springboard。
  • Classes:指定 Class 為作用對象,例:NSString。
  • Executables:指定可執行檔為作用對象,例:callservicesd。

這三類陣列可以混合使用,但當Filter下有不同類型的陣列時,需要添加鍵值Mode = "Any"

{
  Filter = {
      Bundles = ("com.apple.springboard", "com.apple.UIKit");
      Executables = ("callservicesd");
      Mode = "Any";
  };
}

control

此檔案用於紀錄 deb 安裝包管理系統所需的基本資訊。

Package: tw.yuripe-dev.hello
Name: hello
Depends: mobilesubstrate
Version: 0.0.1
Architecture: iphoneos-arm
Description: An awesome MobileSubstrate tweak!
Maintainer: yuripeyamashita
Author: yuripeyamashita
Section: Tweaks
  • Package:該安裝包的唯一識別名稱,可按需求更改。
  • Name:該擴充套件的名稱,可按需求更改。
  • Depends:該安裝包的依賴。此擴充套件運行的基本條件,可填寫系統版本或其它程式,可按需求更改。
    • 例:firmware(>=12.0), mobilesubstrate表示設備 iOS 版本必須在 12 以上,且需安裝 CydiaSubstrate 才能正常運行。
  • Version:該安裝包的版本號,可按需求更改。
  • Architecture:該安裝包的目標設備架構,勿更改。
  • Description:該安裝包的簡介,可按需求更改。
  • Maintainter:該安裝包的維護者,可按需求更改。
  • Author:該擴充套件的作者,可按需求更改。
  • Section:該安裝包的程式類別,勿更改。

Makefile

此檔案用於調整編譯相關設定。

於開頭加入 IP 和埠號並將Tweak.x改為Tweak.xm後存檔:

THEOS_DEVICE_IP = localhost
THEOS_DEVICE_PORT = 2222

INSTALL_TARGET_PROCESSES = SpringBoard

include $(THEOS)/makefiles/common.mk

TWEAK_NAME = hello
hello_FILES = Tweak.xm
hello_CFLAGS = -fobjc-arc

include $(THEOS_MAKE_PATH)/tweak.mk

Tweak.xm

將檔案Tweak.x重新命名為Tweak.xm

此檔案用於撰寫主程式碼,副檔名 x 表示支援 Logos 語法,m 表示支援 C++ 語法。

將其中的內容替換成以下程式碼並存檔:

%hook SpringBoard //指定要 hook 的類別,需要以 %end 結尾
- (void)applicationDidFinishLaunching:(id)arg1 //指定要 hook 的函式
{
  %orig; //呼叫原始函式

  UIAlertView * alert =
  [[UIAlertView alloc] initWithTitle:@"Hello"
                             message:@"It is my first tweak."
                            delegate:self
                   cancelButtonTitle:@"OK"
                   otherButtonTitles:nil];
  [alert show];
  // [alert release];
  // ARC 預設開啟,不用手動 release
}
%end

編譯

cd到專案目錄,於終端機下指令:

make

成功編譯:

> Making all for tweak hello…
==> Preprocessing Tweak.xm…
==> Compiling Tweak.xm (armv7)…
==> Linking tweak hello (armv7)…
==> Generating debug symbols for hello…
rm /Users/yuripeyamashita/Desktop/hello/.theos/obj/debug/armv7/Tweak.xm.mm
==> Preprocessing Tweak.xm…
==> Compiling Tweak.xm (arm64)…
==> Linking tweak hello (arm64)…
==> Generating debug symbols for hello…
rm /Users/yuripeyamashita/Desktop/hello/.theos/obj/debug/arm64/Tweak.xm.mm
==> Preprocessing Tweak.xm…
==> Compiling Tweak.xm (arm64e)…
==> Linking tweak hello (arm64e)…
==> Generating debug symbols for hello…
rm /Users/yuripeyamashita/Desktop/hello/.theos/obj/debug/arm64e/Tweak.xm.mm
==> Merging tweak hello…
==> Signing hello…

至此,已完成範例擴充套件 hello 的編寫。