简体   繁体   中英

Detect whether iOS app is running Debug or Release build without preprocessor macros, for unit testing purposes?

I've seen plenty of answers and am familiar with using something preprocessor macros to check whether I'm in debug or release mode, but I'm keen to unit test around those scenarios, so I'd love a way to check that that I could mock.

Can we detect debug, ad hoc or release without using an #if defined ?

You can have a key configuration in info.plist with value ${CONFIGURATION} . Make sure path for your plist file is set in you project build settings. See images below for reference.

plist图像

info.plist链接图像

You can then access infoDictionary to get the configuration. Code is below

let bundle : NSBundle = NSBundle.mainBundle()
let configuration : String = (bundle.infoDictionary!["configuration"] as! String)  // configuration is key you set in plist file

configuration will be a string storing build configuration set in your scheme.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM