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!
-
this was really helpfull! in my terms of Swift usage! thankyou – Rizwan Atta Jan 26 '20 at 11:27
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 :) .

- 437
- 3
- 14
-
I dont see that. I wish you added an image to make things clearer. – Rohit Singh Jun 16 '23 at 17:55
- 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

- 31
- 2
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 fromiTunes
to see all console events.Note: In the .plist file make sure that Application supports
iTunes file sharing
exists and is set toYES
so that you can access throughiTunes
.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

- 9,494
- 3
- 20
- 29
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.

- 158
- 2
- 17