我想以编程方式确定iOS应用程序是否直接从XCode运行(无论是在模拟器中还是在连接的设备上)。 我试过-D DEBUG解决方案描述here,但是当我从Xcode断开连接并重新运行应用程序时,它仍认为它处于调试模式。 我想我正在寻找的是this function检测是否从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,所以你不需要翻译它。 –
马丁,这是我要说的答案,把它作为答案,我会加快它。 – Knight0fDragon
下面是将代码转换为Swift的快速,未经测试的尝试(仅供趣):https://gist.github.com/getaaron/8d48489274a873835636。我现在没有时间进一步玩,但也许它会让你开始。 –