感谢用户 ahruss 提供的非常有用的链接,这是我找到的解决方案:
我使用this question 中引用的方法创建了一个 main.swift 文件。然后我创建了一个包含此方法定义的 c 文件(和头文件):
typedef int (*command_ptr_t)(int _request, pid_t _pid, caddr_t _addr, int _data);
#if !defined(PT_DENY_ATTACH)
#define PT_DENY_ATTACH 31
#endif
//Anti-debug method
void disable_attach() {
void* handle = dlopen(0, RTLD_GLOBAL | RTLD_NOW);
command_ptr_t command_ptr = dlsym(handle, "ptrace");
command_ptr(PT_DENY_ATTACH, 0, 0, 0);
dlclose(handle);
}
我在桥接头中添加了 disableAttach.h 头文件,然后在 main.swift 中的 UIApplicationMain(Process.argc, Process.unsafeArgv, nil, NSStringFromClass(AppDelegate)) 调用上方直接调用 disable_attach()。
您最终应该得到一个类似于此的 main.swift 文件:
import Foundation
import UIKit
disable_attach()
UIApplicationMain(
CommandLine.argc,
UnsafeMutableRawPointer(CommandLine.unsafeArgv)
.bindMemory(
to: UnsafeMutablePointer<Int8>.self,
capacity: Int(CommandLine.argc)),
nil,
NSStringFromClass(AppDelegate.self)
)
正如我之前在评论中所说,生命周期似乎是相同的,但 @UIApplicationMain 指令隐藏了 main 文件本身。
更新:从 Xcode 10\iOS 12 开始,main.swift 文件应如下所示:
UIApplicationMain(
CommandLine.argc, CommandLine.unsafeArgv,
nil, NSStringFromClass(AppDelegate.self)
)
感谢here 和here 的回答