我想以编程方式确定是否直接从XCode(在模拟器中或在系留设备上)运行iOS应用。我尝试了此处描述的-DDEBUG解决方案,但是当我断开与Xcode的连接并重新运行该应用程序时,它仍然认为它处于调试模式。我认为我正在寻找的是此功能的Swift版本
#include <assert.h> #include <stdbool.h> #include <sys/types.h> #include <unistd.h> #include <sys/sysctl.h> static bool AmIBeingDebugged(void) // Returns true if the current process is being debugged (either // running under the debugger or has a debugger attached post facto). { int junk; int mib[4]; struct kinfo_proc info; size_t size; // Initialize the flags so that, if sysctl fails for some bizarre // reason, we get a predictable result. info.kp_proc.p_flag = 0; // Initialize mib, which tells sysctl the info we want, in this case // we're looking for information about a specific process ID. mib[0] = CTL_KERN; mib[1] = KERN_PROC; mib[2] = KERN_PROC_PID; mib[3] = getpid(); // Call sysctl. size = sizeof(info); junk = sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, NULL, 0); assert(junk == 0); // We're being debugged if the P_TRACED flag is set. return ( (info.kp_proc.p_flag & P_TRACED) != 0 ); }
说明: 您的C代码(以及下面的Swift版本)检查程序是否在 调试器的控制 下运行 , 而不是是否从Xcode运行。一个人可以在Xcode之外调试程序(直接调用lldb或gdb),一个人可以在不调试的情况下从Xcode运行程序(如果scheme设置中的“ Debug Executable”复选框已关闭)。
您可以简单地保留C函数并从Swift调用它。
但是将代码转换为Swift实际上并不复杂:
func amIBeingDebugged() -> Bool { var info = kinfo_proc() var mib : [Int32] = [CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid()] var size = strideofValue(info) let junk = sysctl(&mib, UInt32(mib.count), &info, &size, nil, 0) assert(junk == 0, "sysctl failed") return (info.kp_proc.p_flag & P_TRACED) != 0 }
备注:
kinfo_proc()
info.kp_proc.p_flag = 0
int
Int32
sizeof(info)
strideOfValue(info)
sizeofValue(info)
Swift 3(Xcode 8)更新:
strideofValue并且相关功能已不存在,已被替换为MemoryLayout:
strideofValue
MemoryLayout
func amIBeingDebugged() -> Bool { var info = kinfo_proc() var mib : [Int32] = [CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid()] var size = MemoryLayout<kinfo_proc>.stride let junk = sysctl(&mib, UInt32(mib.count), &info, &size, nil, 0) assert(junk == 0, "sysctl failed") return (info.kp_proc.p_flag & P_TRACED) != 0 }