9

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!

Bappaditya
  • 9,494
  • 3
  • 20
  • 29
Mumtaz Hussain
  • 925
  • 9
  • 23

4 Answers4

19

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 :) .

enter image description here

vishnu_146
  • 437
  • 3
  • 14
3
  • 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
Silly Man
  • 31
  • 2
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 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

Bappaditya
  • 9,494
  • 3
  • 20
  • 29
0

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.

cherry_4
  • 158
  • 2
  • 17