iOS-检测应用程序是否正在从Xcode运行 [英] iOS - detect if app is running from Xcode
问题描述
我正在尝试根据是通过USB / Xcode(调试)运行代码还是从App Store下载(正式版)生产模式来启用/禁用部分代码。我知道要检查它是否以 DEBUG
或 RELEASE
模式运行,如下所示:
I'm trying to enable/disable parts of my code based on whether or not the code be being run via USB/Xcode (debug), or in production mode downloaded from the app store (release). I'm aware of checking if it is running in DEBUG
or RELEASE
mode like this:
#ifdef DEBUG
// Stuff for debug mode
#else
// Stuff for release mode
#endif
但是问题是显而易见的我看到的漏洞是,您可以将运行构建方案的构建配置从调试更改为发布。更好的方法是,如果我可以简单地检测它是否从Xcode运行。我还没有找到一种方法来检查这一点。
but the problem is that an obvious loop-hole I see is you can change the Build Configuration for the 'Run' build scheme from 'Debug' to 'Release'. A better way would be if I can simply detect if it is running from Xcode or not. I haven't found a way to check for this.
是否可以检查iOS应用程序是否从Xcode运行? >
推荐答案
您可以使用检查是否已连接调试器(可能但并非绝对是Xcode) sysctl
。这是 HockeyApp做到的:
#include <Foundation/Foundation.h>
#include <sys/sysctl.h>
/**
* Check if the debugger is attached
*
* Taken from https://github.com/plausiblelabs/plcrashreporter/blob/2dd862ce049e6f43feb355308dfc710f3af54c4d/Source/Crash%20Demo/main.m#L96
*
* @return `YES` if the debugger is attached to the current process, `NO` otherwise
*/
- (BOOL)isDebuggerAttached {
static BOOL debuggerIsAttached = NO;
static dispatch_once_t debuggerPredicate;
dispatch_once(&debuggerPredicate, ^{
struct kinfo_proc info;
size_t info_size = sizeof(info);
int name[4];
name[0] = CTL_KERN;
name[1] = KERN_PROC;
name[2] = KERN_PROC_PID;
name[3] = getpid(); // from unistd.h, included by Foundation
if (sysctl(name, 4, &info, &info_size, NULL, 0) == -1) {
NSLog(@"[HockeySDK] ERROR: Checking for a running debugger via sysctl() failed: %s", strerror(errno));
debuggerIsAttached = false;
}
if (!debuggerIsAttached && (info.kp_proc.p_flag & P_TRACED) != 0)
debuggerIsAttached = true;
});
return debuggerIsAttached;
}
这篇关于iOS-检测应用程序是否正在从Xcode运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!