Why Remove NSLog ?

Leaving NSLog on your production app expose you to potential security risk.

” On the iPhone, data passed to the NSLog function is logged by Apple System Log (ASL) and the data remains on the log until the device is rebooted. Also, Error logs are not bounded by the application sandbox. Which means error logs generated by one application can be read by other applications. Therefore, if an application logs sensitive data, a malicious application can actively query for this data and send it to a remote server.

Error logs on the iPhone can be viewed directly using Console app. The Console app is available in the AppStore. Error logs can also be viewed using iPhone configuration utility or by syncing the device with iTunes and looking at CrashReporter folder “.


How to disable NSLog only on production app

I googled a bit and found most of the answer on Stack Overflow requires changes in existing code, the step below doesn’t require modification of existing code.

Step 1 - Create a .pch file

First in your project, right-click the project folder on the left navigation bar, select New File, scroll down to Others section and choose PCH File .
What is a PCH file?

Type in the filename you want for the pch file, you will need to use the filename for upcoming step.

Step 2 - Define prefix header in project settings

In your project settings, select your app in the Targets and select the Build Settings tab. Search for prefix header section, and type in $(SRCROOT)/$(PRODUCT_NAME)/filename_of_pch.pch for both Debug and Release.

$(SRCROOT) means the path to your Xcode project.
$(PRODUCT_NAME) means the name of the folder of your project.

Step 3 - Edit the .pch file

In the .pch file, add these code at the end of the file.

#ifndef DEBUG
#define NSLog(...) /* replace NSLog method with nothing/blank */

These code means that if the DEBUG flag is not defined (in release mode), it will redefine NSLog method as nothing (means nothing will run). The (…) stands for ‘any text’ between the brackets ().

By default, Xcode will define the DEBUG flag for debug build (when building to simulator/iphone plugged in). You can verify this by going to Project Settings -> Build Settings tab -> Scroll to Preprocessor Macros and you will see the DEBUG=1 is only set for Debug mode. debug macro

For Testflight, Adhoc distribution and App store distribution, the app will compile using Release mode.

Step 4 - Build and run the app in Release mode

So now you have added the .pch file, lets confirm that NSLog statements will not appear in Release mode.

Hold Alt key and press the build button.
A window displaying build options for the target scheme should appear.

Select Release for the Build Configuration and click Run to build and run the app in release mode, you should not see any NSLog statement in the console now. This mean your app in App Store/ Testflight/ Adhoc won’t show any NSLog statement, Yay!

Now you can continue the development without worrying about NSLog appearing on production!

Remember to repeat the step above and revert back to Debug for development.

p/s: I have moved to a new blog https://fluffy.es , you can learn more iOS development stuff there. Also my free ebook about Auto Layout is here.