[英]Is it possible to determine iOS target programmatically
I would like to be able to switch certain functionality on/off in an iOS app depending on whether it is the release version or not. 我希望能够根据iOS版本是否为发行版来打开/关闭某些功能。 Is it possible to programmatically whether the current build is the release version or not? 是否可以通过编程方式来确定当前版本是否为发行版? I know similar functionality can be achieved through the use of macros, but as I understand it these would not work if the code in question is inside a static library. 我知道可以通过使用宏来实现类似的功能,但是据我所知,如果所讨论的代码在静态库中,则这些功能将不起作用。
create a flag in your scheme and use it like 在您的方案中创建一个标志并像使用它
#ifdef BETA
so say you want to have a string method returning two different strings for two different states 所以说你想有一个字符串方法为两个不同的状态返回两个不同的字符串
- (NSString *)someString {
#ifdef BETA
return @"Beta String";
#else
return @"Release String";
#endif
}
You could use the built in 您可以使用内置的
#ifdef DEBUG
this would differentiate between release and debug 这将在发布和调试之间进行区分
I wouldnt recommend having two different targets. 我不建议有两个不同的目标。
As H2CO3 said: 正如H2CO3所说:
#ifdef DEBUG
NSLog(@"Debug mode");
#endif
Just set Target name in your Scheme -> Environment Variables -> add Name and Value. 只需在方案->环境变量->添加名称和值中设置目标名称。 eg: targetName = "mytesttarget" 例如:targetName =“ mytesttarget”
Obj-c OBJ-C
NSDictionary* envir = [[NSProcessInfo processInfo] environment];
NSString* targetName = envir[@"targetName"];
Swift 迅速
let envir = NSProcessInfo.processInfo().environment
let targetName = envir["targetName"]
Now you can check the target condition
if targetName == "mytesttarget" {
...
} else {
...
}
Swift 4: 斯威夫特4:
let envir = ProcessInfo.processInfo.processName
let targetName = envir["targetName"]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.