Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 676
Mumtaz Hussain Avatar asked Dec 26 '18 10:12

Mumtaz Hussain


People also ask

How do I debug an app in Xcode?

When you run an application in Xcode, the debugger is automatically started and attached to the process of the application. Click the Run button in the top left or press Command + R. From the moment the application is up and running, we can start inspecting the process and, if necessary, debug it.

Which method is called when app is killed iOS?

On iOS, nothing gets called when your app gets killed while in the background. At the point where you move from foreground to background, you are supposed to store all the information that is needed for your app to come back looking unchanged if it gets killed and launched again.

What is debug executable in Xcode?

The “Debug executable” checkbox specifies whether or not you want to run with the debugger enabled. Once running, you can use Debug > Attach to Process on a process that has been launched with debugging disabled if needed. It seems like all this does is start your app with the debugger attached.


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

like image 178
vishnu_146 Avatar answered Oct 04 '22 17:10

vishnu_146


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

like image 20
Bappaditya Avatar answered Oct 04 '22 16:10

Bappaditya