我想以編程方式確定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。我現在沒有時間進一步玩,但也許它會讓你開始。 –