Removing NSLogs for distribution

Guides | Tutorial By 5 years ago

When developing an iOS or Mac app, NSLogs are very handy during development. These are quite useless for an app on an end users phone or computer, so removing every log is often a good idea, especially if you like to log a lot of information. This might sound like a pain, commenting out every NSLog whenever you build for the AppStore? But no, there’s a much simpler solution, coming from our friend the preprocessor macro.

Every iOS and Mac project has a special file called <your project>-Prefix.pch. What does this mean? This is a precompiled header – a .h that is imported by every file in your project without the need to manually #import it. Combining this and preprocessor macros gives a very elegant solution to removing logs when building for the AppStore, and keeping them when developing.

Just put this into your pch:

#warning NSLogs will be shown
#define NSLog(...) {}

Now try and build for development, you will get a warning telling you that logs will be shown. Build again for release and the macro replaces every NSLog (at compile time) with nothing.

  • Hemant Bosamia

    Hey Tom, just awesome. What a brilliant way to remove NSlogs. Thank you.