How to Debug an app after it is terminated and then reopened in Xcode?

Viewed 5832

I usually do debugging with support of print() method at which shows on Xcode logs as long as it is not terminated. However I have some conditions that I need to test in didFinishLaunchingWithOptions method of AppDelegate when the app's been terminated and then reopened. By "reopened" I mean by clicking on the app on simulator/iphone instead of running it from Xcode again. Sadly after termination print logs do not show. Any other way I could do it? Thanks!

4 Answers

Click on the options near the Appname on the upper-left corner of Xcode.

Click on Edit Scheme -> Check the Wait for executable to launch option and run as you usually do. Happy Coding :) .

  • Check "Wait for executable to launch" as @vishnu_146 mentioned.
  • CMD + R to run the app (first launch)
  • Double press Home and kill the app
  • CMD + R to run the app again (I tested that run without build will fresh launch the app).
  • App open with previous status. Can hit break points, but could not print logs. Reason: NSLog not working when "wait for executable to be launched" is set

In Swift 4.2,

var paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let documentsDirectory = paths[0]
let fileName = "\(Date()).log"
let logFilePath = (documentsDirectory as NSString).appendingPathComponent(fileName)
freopen(logFilePath.cString(using: String.Encoding.ascii)!, "a+", stderr)

Just add this block of code in application:didFinishLaunchingWithOptions method in the app delegate file and it will create a log file in app document directory on iPhone which logs all console log events. You need to import this file from iTunes to see all console events.

Note: In the .plist file make sure that Application supports iTunes file sharing exists and is set to YES so that you can access through iTunes.

To get Logfiles: Launch iTunes, after your device has connected select Apps - select your App - in Augmentnt Document you will get your file. You can then save it to your disk

You can try printing logs through "NSLog". On Xcode, go to "Devices and Simulators" and select your device. All the NSLogs will be visible there at the bottom.

Related