The cost of learning and using SOT is mainly concentrated in the early stage and mainly involves the modification of compilation process. The previous section described how to access “OC Access Example” for a pure OC project. This article describes how to access SOT for pure Swift projects, including the free version and the website version.

This article takes the open source SwiftMessages Demo as an example, which is all developed in Swift language. Here, the modified version is uploaded to Git, and the branch is “sotdemo”. The free version is connected in Debug mode, and the website version is connected in Release mode. Readers can also directly use this branch to test.

Clone the original project, command line CD go to the root directory, use the version switch command:git checkout 1e49de7b3780b699(Since this document was produced on October 21, 21, the current version shall prevail.) Xcodeproj = demo.xcodeProj = demo.xcodeProj = demo.xcodeProj = demo.xcodeProj = demo.xcodeProj = demo.xcodeProj

I used Xcode12.4, which can directly compile successfully, and you can see the picture (simulator) after starting the APP:

Click on the top oneMESSAGE VIEWControl, will pop up an error message window, today we will use the SOT heat more way to modify the error message copywriting:

Step1: configure the compilation environment

reference”Free Version”Step1 to step3, step3 copy sotconfig.sh into the project Demo directory:

Open sotconfig.sh in a text editor and modifyEnableSot=1:

Step2: modify compilation options

Add more needed compilation options, add SOT virtual machine static library, etc., as follows:

  1. Select the Demo project, then select the Demo Target, and then select Build Settings:

  2. Add -sotModule $(PRODUCT_NAME) /Users/ sotsdK-1.0 /libs/libsot_free.a -sotsaved in Other Linker Flags $(SRCROOT)/sotsaved/$(CONFIGURATION)/$(CURRENT_ARCH) -sotconfig $(SRCROOT)/sotconfig.sh, the meaning of each option is as follows:

    • – sotModule is the name of the module. You can use $(PRODUCT_NAME) or a user-defined name.
    • -sotsaved Is the directory for saving the compiled intermediate products. The automatic generation of patches requires comparison of the compiled products before and after the generation of patches.
    • -sotconfig specifies the path to the project sotconfig.sh, which controls the work of the SOT compiler and is referenced to with $(SRCROOT)
    • / Users/sotsdk - 1.0 / libs/libsot_free. AIs the path to the SOT static library, which is linked to the free VERSION of the VM
  3. inOther C FlagsAs well asOther Swift Flagsadd-sotmodule $(PRODUCT_NAME) -sotconfig $(SRCROOT)/sotconfig.sh, meaning is the same as the previous step, need to keep consistent. After the above two steps, the relevant compilation and configuration results are shown as follows:

  4. Since the SOT SDK library file is compiled without Bitcode, you also need to set Target’s Enable Bitcode toNo.


Step3: add patch copy scripts

The SDK provides a convenience script in the project-script/sot_package.sh directory of the SDK, which copies the generated patches to the Bundle folder and calls the script every time the project is successfully compiled. Add the following steps:

The script content is as follows:Sh/Users/sotsdk - 1.0 / project - the script/sot_package. Sh "$SOURCE_ROOT/sotconfig. Sh" "$SOURCE_ROOT sotsaved / $CONFIGURATION" Demo

Uncheck the check box Based on Dependency Analysis


Step4: link the C++ library

SOT requires the support of the c++ standard library and the Link Binary With Libraries page

Hit the plus sign, add these two,libz.tbdandlibc++.tbd


Step5: call the SDK API

The OC code needs to be called by Swift code. A sample code has been provided in the Swift -call-objc directory of the SDK, which can be added directly to the Demo project. Click the File button of Xcode software, then click Add Files to “Demo”, as shown below:

Select swift-call-objc in SDK directory, select callsot.h and callsot.m at the same time, check Copy items if needed below, check Demo target in Add to Targets:, as shown below:

Click the Add button and a question will pop up asking if you want to create a bridge file. Click on the button Create Bridging Header:

H, callsot.m, and demo-bridge-header. h:

Open demo-bridge-header. h and add a line of code#import "callsot.h":

Open Appdelegate.Swift and add two lines of codelet sot = CallSot()andsot.initSot()


Test the heat more

Step1: heat injection

After the above configuration, make sure that the sotconfig.sh configuration is,EnableSot=1As well asGenerateSotShip=0Clean Build Folder first and then Build:

Run sot Link, etc., will tell you which functions in each file can be hot changed, etc. :

The project is compiled successfully, and the APP can be started normally. It also has the capability to load patches to change the code logic of the program. Here is how to generate patches to test it.


Step2: generate patches

The previous step was to compile the heat injection code, which was saved to the Demo/ sotSaved folder to compare with the new code to generate a patch. The procedure for generating a patch is as follows:

  1. Start the SOT patch generation mode and change sotconfig.sh to EnableSot=1 and GenerateSotShip=1
  2. The viewController.swift file “Something is horribly wrong! “SOT is great” :
  3. Generating patches is not the same as OC projects,Every time you need to Clean the project, then Build the project. Link Demo (x86_64) builds a patch and copies it to the Bundle directory.The demoBasics function has been modified:
  4. The original generated patch file is saved to Demo/ SOtsaved /Debug/x86_64/ship/ship.sot. Remember adding a script to Build Phase earlier? It copies the patch to the Bundle directory at the end of each build and adds the CPU architecture to the file name. You can see the patch in the Bundle, and that’s it.

Step3: load the patch

When the APP is started, the API will determine whether there is a patch in the Bundle, and if there is, the Bundle will be loaded. The successfully loaded log is similar to the following, indicating that a module has loaded a hot update patch:Then click on the top oneMESSAGE VIEWControl, find that the popup copy becomes SOT is great:

If you go to the Xcode breakpoint to debug demoBasics, you will find that it cannot be broken because it is the SOT virtual machine that actually performs the patch code.

By the way, when GenerateSotShip=1, you’re compiling your APP using code that’s stored in the SOtSaved directory, so no matter how much you change the code in Xcode, if you don’t copy the patch into the Bundle directory, So your APP is going to look like the last time you did the hot injection with GenerateSotShip=0.

If in doubt, you can remove the Script that copied the patch from Build Phases and see that no changes will take effect.