Remove logs completely from iOS release build (also from compiled binary file)

Viewed 304

Is there any way to completely remove logs from a final release build of an application written in Swift? By completely I mean that if I open the compiled binary file in a text/hex editor there should be no logged strings in it and if someone disassembles the binary file also he should not see any logged strings in a disassembler.

I know that I can disable writing the logs to the console in release build with #if DEBUG for example with something like this:

func log(_ message: String) {
    #if DEBUG
    print(message)
    #endif
}

But when using this approach logged strings are still visible in compiled binary (and could for example help someone in analysing disassembled code of the application). They are just not printed on the console and that is not acceptable for me. I want to remove them completely from compiled binary. In Objective-C we could use preprocesor macros to achieve that but they are not available in Swift. I could put #if DEBUG before each occurrence of log/print function in the code and that would work but obviously this is not a great solution as it would require to add this #if DEBUG in hundreds of places. Alternatively I could just do find and replace to comment all print/log calls before building a release build but this is also not a great solution as I would need to comment and uncomment those logs each time I build the release version. Is there any better way to achieve that?

1 Answers

You could try using the the Logger framework provided by Apple

It is more performant that print statements, and you have more control over what is logged - for developing and debugging you can use the debug log level. There are other logging levels that generate messages during run time with privacy options.

There is a WWDC Video which you may find useful to introduce you to this.

Related