Work with Objective-C in iOS emulation
mainFor interacting with Objective-C objects, use the ObjcRuntime class, passing your Chomper instance to it.
Key workflows:
- Memory Management: Wrap Objective-C operations in
objc.autorelease_pool()to ensure objects are automatically released. - Class Discovery: Use
find_class(name)to locate an Objective-C class. - Object Creation: Use
create_ns_string(string)to create anNSStringobject. - Method Invocation: Use
class_instance.call_method(selector, *args)to call methods. - Data Conversion: Use
call_method("UTF8String")on anNSStringobject to get a C string pointer, then useemu.read_string(ptr)to read it into Python.
from chomper import Chomper
from chomper.const import ARCH_ARM64, OS_IOS
from chomper.objc import ObjcRuntime
emu = Chomper(
arch=ARCH_ARM64,
os_type=OS_IOS,
rootfs_path="rootfs/ios",
)
objc = ObjcRuntime(emu)
emu.load_module("examples/binaries/ios/cn.com.scal.sichuanair/zsch")
# Use autorelease_pool for automatic memory management
with objc.autorelease_pool():
# Find class
zsch_rsa_class = objc.find_class("ZSCHRSA")
# Create NSString object
input_str = objc.create_ns_string("Mocha")
# Call Objective-C method
req_sign = zsch_rsa_class.call_method("getReqSign:", input_str)
# Convert NSString object to C string and read it
result_ptr = req_sign.call_method("UTF8String")
result = emu.read_string(result_ptr)