【发布时间】:2016-01-15 14:27:16
【问题描述】:
我想以编程方式确定 iOS 应用程序是否直接从 XCode 运行(在模拟器中或在连接的设备上)。 我已经尝试了here 描述的 -D DEBUG 解决方案,但是当我断开与 Xcode 的连接并重新运行应用程序时,它仍然认为它处于调试模式。 我想我正在寻找的是this function的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 );
}
【问题讨论】:
-
你可以从 Swift 中调用 C 函数,所以你不需要翻译它。
-
马丁,这就是我要提出的答案,把它作为答案,我会投票赞成。
-
这是一个快速的、未经测试的将代码转换为 Swift 的尝试(只是为了好玩):gist.github.com/getaaron/8d48489274a873835636。我现在没有时间进一步玩它,但也许它会让你开始。
-
@Knight0fDragon:将其翻译成 Swift 是更有趣的挑战 :)